ip-address PHP

[Solved –10 Answers] How to get the client IP address in PHP

[Solved –10 Answers] PHP - How to get the client IP address in PHP? - How can we able to get the client IP address using PHP? We want to keep record of

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.
php code
// Get the client IP address
$ip = $_SERVER['REMOTE_ADDR‘]

we can use different server super global functions to check valid IP address.(http client IP and http x forwarded for, remote address)

php code
$http_client_ip = $_SERVER['HTTP_CLIENT_IP']; //Internet ip address
$http_x_forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR']; //checking for proxy server
$remote_addr = $_SERVER['REMOTE_ADDR']; //
if(!empty($http_client_ip)){
$ip = $http_client_ip;
}elseif(!empty($http_x_forwarded_for)){
$ip = $http_x_forwarded_for;
}else{
$ip = $remote_addr;
}
echo $ip;

SOLUTION 2: 

Sample code:

php code
if (!empty($_SERVER['HTTP_CLIENT_IP']))
 { $ip = $_SERVER['HTTP_CLIENT_IP']; 
}
 elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) 
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; 
}
 else
 {  
$ip = $_SERVER['REMOTE_ADDR'];
 } 

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.

SOLUTION 3: 

Try this :

php code
echo $_SERVER['REMOTE_ADDR']; 

SOLUTION 4: 

Sample code:

php code
$ip = $_SERVER['HTTP_CLIENT_IP']?$_SERVER['HTTP_CLIENT_IP']:($_SERVER['HTTP_X_FORWARDE‌​D_FOR']?$_SERVER['HTTP_X_FORWARDED_FOR']:$_SERVER['REMOTE_ADDR']); 

Here is a shorter version that uses the elvis operator

php code
$_SERVER['HTTP_CLIENT_IP']?:($_SERVER['HTTP_X_FORWARDE‌​D_FOR']?:$_SERVER['REMOTE_ADDR']); 

SOLUTION 5: 

  • This is the method validates an IPv4 input:
php code
// Get user IP address 
if ( isset($_SERVER['HTTP_CLIENT_IP']) && ! empty($_SERVER['HTTP_CLIENT_IP'])) {
 $ip = $_SERVER['HTTP_CLIENT_IP']; 
}
 elseif ( isset($_SERVER['HTTP_X_FORWARDED_FOR']) && ! empty($_SERVER['HTTP_X_FORWARDED_FOR']))
 {
 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
 } else {
 $ip = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0'; 
}
 $ip = filter_var($ip, FILTER_VALIDATE_IP); 
$ip = ($ip === false) ? '0.0.0.0' : $ip; 

SOLUTION 6: 

  • Another method of implementation:
php code
$ip = ""; 
if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
 //check for ip from share internet $ip = $_SERVER["HTTP_CLIENT_IP"]; 
} elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) 
{ 
// Check for the Proxy User 
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; 
} else
 { 
$ip = $_SERVER["REMOTE_ADDR"];
 } 
echo $ip; 

SOLUTION 7: 

There are different type of users behind the Internet, So we want to catch the IP address from different potions. They are,

See also  [Solved -8 Answers] PHP-JAVASCRIPT - PHP Constants Containing Arrays

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

php code
// Function to get the user IP address 
function getUserIP() { $ipaddress = ''; 
if (isset($_SERVER['HTTP_CLIENT_IP']))
     $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
     $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_X_FORWARDED']))
     $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if(isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP'])) 
     $ipaddress = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
     $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; 
else if(isset($_SERVER['HTTP_FORWARDED']))
    $ipaddress = $_SERVER['HTTP_FORWARDED']; 
else if(isset($_SERVER['REMOTE_ADDR']))
     $ipaddress = $_SERVER['REMOTE_ADDR']; 
else 
     $ipaddress = 'UNKNOWN'; 
return $ipaddress;
 }


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’]);

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:
php code
<?php
 $ip='0.0.0.0';
 $ip=$_SERVER['REMOTE_ADDR']; 
$clientDetails = json_decode(file_get_contents("http://ipinfo.io/$ip/json")); 
echo "You're logged in from: <b>" . $clientDetails->country . "</b>"; 
?> 
  • 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
php code
function getClientIP() { 
if (isset($_SERVER))
 {
 if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) 
    return $_SERVER["HTTP_X_FORWARDED_FOR"]; 

if (isset($_SERVER["HTTP_CLIENT_IP"]))
   return $_SERVER["HTTP_CLIENT_IP"]; 

   return $_SERVER["REMOTE_ADDR"];
 }
 if (getenv('HTTP_X_FORWARDED_FOR')) 
    return getenv('HTTP_X_FORWARDED_FOR');

 if (getenv('HTTP_CLIENT_IP')) 
    return getenv('HTTP_CLIENT_IP');

     return getenv('REMOTE_ADDR'); 
} 

About the author

Wikitechy Editor

Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.

Add Comment

Click here to post a comment