240

I want to get the client IP address who uses my website. I am using the PHP $_SERVER superglobal:

$_SERVER['REMOTE_ADDR']; 

But I see it can not give the correct IP address using this. I get my IP address and see it is different from my IP address and I can also see my IP address in some website like:

http://whatismyipaddress.com/ 

I paste the IP address which give my PHP function but this website shows no result about this. How does this problem come about and how can I get IP address of the client?

4
  • 5
    stackoverflow.com/questions/1634782/… Commented Mar 29, 2013 at 7:26
  • 2
    If you are on a local server it will be different (eg: 192.168.xxx.xxx), because you check from whatsmyip you are getting your isp ip they supplied to you. Commented Mar 29, 2013 at 7:27
  • On your computer you'll see your private IP (192..) and on websites you'll your public IP (84...). In general your public IP is the only interesting one. Commented Aug 2, 2013 at 9:25
  • 13
    Again, not really a duplicate, seeing as how this is the best ranked by Google. Stackoverflow guys, come on. "Marked as Duplicate" happens too often. If this ranks better, it's for good reason. Google has spoken. Commented Jan 22, 2015 at 12:02

4 Answers 4

435

The simplest way to get the visitor’s/client’s IP address is using the $_SERVER['REMOTE_ADDR'] or $_SERVER['REMOTE_HOST'] variables.

However, sometimes this does not return the correct IP address of the visitor, so we can use some other server variables to get the IP address.

The below both functions are equivalent with the difference only in how and from where the values are retrieved.

getenv() is used to get the value of an environment variable in PHP.

// Function to get the client IP address function get_client_ip() { $ipaddress = ''; if (getenv('HTTP_CLIENT_IP')) $ipaddress = getenv('HTTP_CLIENT_IP'); else if(getenv('HTTP_X_FORWARDED_FOR')) $ipaddress = getenv('HTTP_X_FORWARDED_FOR'); else if(getenv('HTTP_X_FORWARDED')) $ipaddress = getenv('HTTP_X_FORWARDED'); else if(getenv('HTTP_FORWARDED_FOR')) $ipaddress = getenv('HTTP_FORWARDED_FOR'); else if(getenv('HTTP_FORWARDED')) $ipaddress = getenv('HTTP_FORWARDED'); else if(getenv('REMOTE_ADDR')) $ipaddress = getenv('REMOTE_ADDR'); else $ipaddress = 'UNKNOWN'; return $ipaddress; } 

$_SERVER is an array that contains server variables created by the web server.

// Function to get the client IP address function get_client_ip() { $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_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; } 
Sign up to request clarification or add additional context in comments.

14 Comments

note that this function can also lead you very wrong. in particular, HTTP_X_FORWARDED_FOR can be anything a client chooses to set, whereas REMOTE_ADDR is considerably harder to fake, if client wants to get the actual response.
also, even if it is not faked, there are lots of setups where it will just be populated with clients internal network ip, which would be useless.
this code is vulnerable to spoofing be careful where you use it!
You need to read this before you get hacked:stackoverflow.com/questions/3003145/…
@huykon225 Because You Are in Localhost. Try by uploading script on server.
|
145

In PHP 5.3 or greater, you can get it like this:

$ip = getenv('HTTP_CLIENT_IP')?: getenv('HTTP_X_FORWARDED_FOR')?: getenv('HTTP_X_FORWARDED')?: getenv('HTTP_FORWARDED_FOR')?: getenv('HTTP_FORWARDED')?: getenv('REMOTE_ADDR'); 

11 Comments

This worked for me where as the one marked as correct answer didn't.
getenv returns a false if the variable isn't set, where $_SERVER will error with "undefined index" if the variable isn't set.
I usually don't like nested ternaries, but I really like it in this case...
I tend to avoid ternaries altogether in PHP cause of its (illogical) left-associativity. See phpsadness.com/sad/30
GeriBoss, it's not so hard to figure out a solution to that phpsadness test case and make the code more readable at the same time: echo (FALSE ? "a" : (FALSE ? "b" : "c"))."\n"; echo (FALSE ? "a" : (TRUE ? "b" : "c"))."\n"; echo (TRUE ? "a" : (FALSE ? "b" : "c"))."\n"; echo (TRUE ? "a" : (TRUE ? "b" : "c"))."\n";
|
1
 $ipaddress = ''; if ($_SERVER['HTTP_CLIENT_IP'] != '127.0.0.1') $ipaddress = $_SERVER['HTTP_CLIENT_IP']; else if ($_SERVER['HTTP_X_FORWARDED_FOR'] != '127.0.0.1') $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; else if ($_SERVER['HTTP_X_FORWARDED'] != '127.0.0.1') $ipaddress = $_SERVER['HTTP_X_FORWARDED']; else if ($_SERVER['HTTP_FORWARDED_FOR'] != '127.0.0.1') $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; else if ($_SERVER['HTTP_FORWARDED'] != '127.0.0.1') $ipaddress = $_SERVER['HTTP_FORWARDED']; else if ($_SERVER['REMOTE_ADDR'] != '127.0.0.1') $ipaddress = $_SERVER['REMOTE_ADDR']; else $ipaddress = 'UNKNOWN'; 

Comments

-18

Here is a function to get the IP address using a filter for local and LAN IP addresses:

function get_IP_address() { foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key){ if (array_key_exists($key, $_SERVER) === true){ foreach (explode(',', $_SERVER[$key]) as $IPaddress){ $IPaddress = trim($IPaddress); // Just to be safe if (filter_var($IPaddress, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) { return $IPaddress; } } } } } 

4 Comments

code function getLocalIP(){ exec("ipconfig /all", $output); foreach($output as $line){ if (preg_match("/(.*)IPv4 Address(.*)/", $line)){ $ip = $line; $ip = str_replace("IPv4 Address. . . . . . . . . . . :","",$ip); $ip = str_replace("(Preferred)","",$ip); } } return trim($ip); }
so many negative flags yet only this answer contains FILTER_VALIDATE_IP which can be a major security concern if not filtered.
use this code to find the ip address via php <?php echo $response = file_get_contents('api.ipify.org'); ?>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.