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.
[pastacode lang=”php” manual=”%2F%2F%20Get%20the%20client%20IP%20address%0A%24ip%C2%A0%3D%C2%A0%24_SERVER%5B’REMOTE_ADDR%E2%80%98%5D%0A” message=”php code” highlight=”” provider=”manual”/]

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:
[pastacode lang=”php” manual=”%2F%2F%20Get%20user%20IP%20address%20%0Aif%20(%20isset(%24_SERVER%5B’HTTP_CLIENT_IP’%5D)%20%26%26%20!%20empty(%24_SERVER%5B’HTTP_CLIENT_IP’%5D))%20%7B%0A%20%24ip%20%3D%20%24_SERVER%5B’HTTP_CLIENT_IP’%5D%3B%20%0A%7D%0A%20elseif%20(%20isset(%24_SERVER%5B’HTTP_X_FORWARDED_FOR’%5D)%20%26%26%20!%20empty(%24_SERVER%5B’HTTP_X_FORWARDED_FOR’%5D))%0A%20%7B%0A%20%24ip%20%3D%20%24_SERVER%5B’HTTP_X_FORWARDED_FOR’%5D%3B%0A%20%7D%20else%20%7B%0A%20%24ip%20%3D%20(isset(%24_SERVER%5B’REMOTE_ADDR’%5D))%20%3F%20%24_SERVER%5B’REMOTE_ADDR’%5D%20%3A%20’0.0.0.0’%3B%20%0A%7D%0A%20%24ip%20%3D%20filter_var(%24ip%2C%20FILTER_VALIDATE_IP)%3B%20%0A%24ip%20%3D%20(%24ip%20%3D%3D%3D%20false)%20%3F%20’0.0.0.0’%20%3A%20%24ip%3B%20%0A” message=”php code” highlight=”” provider=”manual”/]

SOLUTION 6: 

  • Another method of implementation:
[pastacode lang=”php” manual=”%24ip%20%3D%20%22%22%3B%20%0Aif%20(!empty(%24_SERVER%5B%22HTTP_CLIENT_IP%22%5D))%20%7B%0A%20%2F%2Fcheck%20for%20ip%20from%20share%20internet%20%24ip%20%3D%20%24_SERVER%5B%22HTTP_CLIENT_IP%22%5D%3B%20%0A%7D%20elseif%20(!empty(%24_SERVER%5B%22HTTP_X_FORWARDED_FOR%22%5D))%20%0A%7B%20%0A%2F%2F%20Check%20for%20the%20Proxy%20User%20%0A%24ip%20%3D%20%24_SERVER%5B%22HTTP_X_FORWARDED_FOR%22%5D%3B%20%0A%7D%20else%0A%20%7B%20%0A%24ip%20%3D%20%24_SERVER%5B%22REMOTE_ADDR%22%5D%3B%0A%20%7D%20%0Aecho%20%24ip%3B%20%0A” message=”php code” highlight=”” provider=”manual”/] [ad type=”banner”]

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:
[pastacode lang=”php” manual=”%3C%3Fphp%0A%20%24ip%3D’0.0.0.0’%3B%0A%20%24ip%3D%24_SERVER%5B’REMOTE_ADDR’%5D%3B%20%0A%24clientDetails%20%3D%20json_decode(file_get_contents(%22http%3A%2F%2Fipinfo.io%2F%24ip%2Fjson%22))%3B%20%0Aecho%20%22You’re%20logged%20in%20from%3A%20%3Cb%3E%22%20.%20%24clientDetails-%3Ecountry%20.%20%22%3C%2Fb%3E%22%3B%20%0A%3F%3E%20%0A” message=”php code” highlight=”” provider=”manual”/]
  • 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
[pastacode lang=”php” manual=”function%20getClientIP()%20%7B%20%0Aif%20(isset(%24_SERVER))%0A%20%7B%0A%20if%20(isset(%24_SERVER%5B%22HTTP_X_FORWARDED_FOR%22%5D))%20%0A%20%20%20%20return%20%24_SERVER%5B%22HTTP_X_FORWARDED_FOR%22%5D%3B%20%0A%0Aif%20(isset(%24_SERVER%5B%22HTTP_CLIENT_IP%22%5D))%0A%20%20%20return%20%24_SERVER%5B%22HTTP_CLIENT_IP%22%5D%3B%20%0A%0A%20%20%20return%20%24_SERVER%5B%22REMOTE_ADDR%22%5D%3B%0A%20%7D%0A%20if%20(getenv(‘HTTP_X_FORWARDED_FOR’))%20%0A%20%20%20%20return%20getenv(‘HTTP_X_FORWARDED_FOR’)%3B%0A%0A%20if%20(getenv(‘HTTP_CLIENT_IP’))%20%0A%20%20%20%20return%20getenv(‘HTTP_CLIENT_IP’)%3B%0A%0A%20%20%20%20%20return%20getenv(‘REMOTE_ADDR’)%3B%20%0A%7D%20%0A” message=”php code” highlight=”” provider=”manual”/]

Categorized in: