PROBLEM :
How can we able to get the client IP address using PHP?
- We want to keep record of the user who logged into our website through IP address.
SOLUTION 1:
- The simplest way is remote address server super global. But this is not the best way to do is using different server super global functions, http client IP and http x forwarded for, remote address.
- By using these three functions we can get correct ip address of a client or computer who is accessing our website.
- Simply use the below code to get client IP address in php.
we can use different server super global functions to check valid IP address.(http client IP and http x forwarded for, remote address)
[pastacode lang=”php” manual=”%24http_client_ip%20%3D%20%24_SERVER%5B’HTTP_CLIENT_IP’%5D%3B%20%2F%2FInternet%20ip%20address%0A%24http_x_forwarded_for%20%3D%20%24_SERVER%5B’HTTP_X_FORWARDED_FOR’%5D%3B%20%2F%2Fchecking%20for%20proxy%20server%0A%24remote_addr%20%3D%20%24_SERVER%5B’REMOTE_ADDR’%5D%3B%20%2F%2F%0Aif(!empty(%24http_client_ip))%7B%0A%24ip%20%3D%20%24http_client_ip%3B%0A%7Delseif(!empty(%24http_x_forwarded_for))%7B%0A%24ip%20%3D%20%24http_x_forwarded_for%3B%0A%7Delse%7B%0A%24ip%20%3D%20%24remote_addr%3B%0A%7D%0Aecho%20%24ip%3B%0A” message=”php code” highlight=”” provider=”manual”/]SOLUTION 2:
Sample code:
[pastacode lang=”php” manual=”if%20(!empty(%24_SERVER%5B’HTTP_CLIENT_IP’%5D))%0A%20%7B%20%24ip%20%3D%20%24_SERVER%5B’HTTP_CLIENT_IP’%5D%3B%20%0A%7D%0A%20elseif%20(!empty(%24_SERVER%5B’HTTP_X_FORWARDED_FOR’%5D))%20%0A%24ip%20%3D%20%24_SERVER%5B’HTTP_X_FORWARDED_FOR’%5D%3B%20%0A%7D%0A%20else%0A%20%7B%20%20%0A%24ip%20%3D%20%24_SERVER%5B’REMOTE_ADDR’%5D%3B%0A%20%7D%20%0A” message=”php code” highlight=”” provider=”manual”/]Note: Using the above code has security implications. The client can set all HTTP header information (ie. $_SERVER[‘HTTP_…) to any arbitrary value it wants. As such it’s far more reliable to use $_SERVER[‘REMOTE_ADDR’], as this cannot be set by the user.
[ad type=”banner”]SOLUTION 3:
Try this :
[pastacode lang=”php” manual=”echo%20%24_SERVER%5B’REMOTE_ADDR’%5D%3B%20%0A” message=”php code” highlight=”” provider=”manual”/]SOLUTION 4:
Sample code:
[pastacode lang=”php” manual=”%24ip%20%3D%20%24_SERVER%5B’HTTP_CLIENT_IP’%5D%3F%24_SERVER%5B’HTTP_CLIENT_IP’%5D%3A(%24_SERVER%5B’HTTP_X_FORWARDE%E2%80%8C%E2%80%8BD_FOR’%5D%3F%24_SERVER%5B’HTTP_X_FORWARDED_FOR’%5D%3A%24_SERVER%5B’REMOTE_ADDR’%5D)%3B%20%0A” message=”php code” highlight=”” provider=”manual”/]Here is a shorter version that uses the elvis operator
[pastacode lang=”php” manual=”%24_SERVER%5B’HTTP_CLIENT_IP’%5D%3F%3A(%24_SERVER%5B’HTTP_X_FORWARDE%E2%80%8C%E2%80%8BD_FOR’%5D%3F%3A%24_SERVER%5B’REMOTE_ADDR’%5D)%3B%20%0A” message=”php code” highlight=”” provider=”manual”/]SOLUTION 5:
- This is the method validates an IPv4 input:
SOLUTION 6:
- Another method of implementation:
SOLUTION 7:
There are different type of users behind the Internet, So we want to catch the IP address from different potions. They are,
1. $_SERVER[‘REMOTE_ADDR’] – It contains the real IP address of the client. This is the most reliable value we can able to find from the user.
2. $_SERVER[‘REMOTE_HOST’] – It will fetch the Host name from which the user is viewing the current page. But for this script to work, Hostname Lookups On inside httpd.conf must be configured.
3. $_SERVER[‘HTTP_CLIENT_IP’] – It will fetch the IP address when user is from Shared Internet services.
4. $_SERVER[‘HTTP_X_FORWARDED_FOR’] – It will fetch the IP address from the user when he is behind the proxy
[pastacode lang=”php” manual=”%2F%2F%20Function%20to%20get%20the%20user%20IP%20address%20%0Afunction%20getUserIP()%20%7B%20%24ipaddress%20%3D%20”%3B%20%0Aif%20(isset(%24_SERVER%5B’HTTP_CLIENT_IP’%5D))%0A%20%20%20%20%20%24ipaddress%20%3D%20%24_SERVER%5B’HTTP_CLIENT_IP’%5D%3B%0Aelse%20if(isset(%24_SERVER%5B’HTTP_X_FORWARDED_FOR’%5D))%0A%20%20%20%20%20%24ipaddress%20%3D%20%24_SERVER%5B’HTTP_X_FORWARDED_FOR’%5D%3B%0Aelse%20if(isset(%24_SERVER%5B’HTTP_X_FORWARDED’%5D))%0A%20%20%20%20%20%24ipaddress%20%3D%20%24_SERVER%5B’HTTP_X_FORWARDED’%5D%3B%0Aelse%20if(isset(%24_SERVER%5B’HTTP_X_CLUSTER_CLIENT_IP’%5D))%20%0A%20%20%20%20%20%24ipaddress%20%3D%20%24_SERVER%5B’HTTP_X_CLUSTER_CLIENT_IP’%5D%3B%0Aelse%20if(isset(%24_SERVER%5B’HTTP_FORWARDED_FOR’%5D))%0A%20%20%20%20%20%24ipaddress%20%3D%20%24_SERVER%5B’HTTP_FORWARDED_FOR’%5D%3B%20%0Aelse%20if(isset(%24_SERVER%5B’HTTP_FORWARDED’%5D))%0A%20%20%20%20%24ipaddress%20%3D%20%24_SERVER%5B’HTTP_FORWARDED’%5D%3B%20%0Aelse%20if(isset(%24_SERVER%5B’REMOTE_ADDR’%5D))%0A%20%20%20%20%20%24ipaddress%20%3D%20%24_SERVER%5B’REMOTE_ADDR’%5D%3B%20%0Aelse%20%0A%20%20%20%20%20%24ipaddress%20%3D%20’UNKNOWN’%3B%20%0Areturn%20%24ipaddress%3B%0A%20%7D%0A%0B%0A” message=”php code” highlight=”” provider=”manual”/]SOLUTION 8:
using the GLOBAL variable named as $_SERVER.
the $_SERVER is an array which has an attribute names REMOTE_ADDR.
Just assign it like this $userIp = $_SERVER[‘REMOTE_ADDR’];
or use it directly like echo $_SERVER[‘REMOTE_ADDR’]; or echo ($_SERVER[‘REMOTE_ADDR’]);
[ad type=”banner”]SOLUTION 9:
- We can able to use $_SERVER[‘REMOTE_ADDR’]; to get client IP address and if we need more information about a user, Use below sample code:
- Client’s more specific info goes in $clientDetails.We can able to fetch json items stored in $clientDetails variable in this way: $clientDetails->PostalCode/hostname/region/loc..
SOLUTION 10:
- Here is the another solution to get the ip address