tags:

views:

986

answers:

5

I would like to find a user's IP address when he/she enters my page. How do I programmatically do that?

+10  A: 

It should be contained in the $_SERVER['REMOTE_ADDR'] variable.

Kyle Cronin
+2  A: 

$_SERVER['REMOTE_ADDR'];

Polygraf
+20  A: 
Tim Kennedy
Since my own question has been closed (http://stackoverflow.com/questions/848091/how-can-i-get-the-users-ip-address-in-php) how can you get the above code to always find the IP address. Using this code, it will fall down to $_SERVER['REMOTE_ADDR'] and get a value of either blank or unknown in over 20% of cases.
Ryaner
+4  A: 

I always forget and do a:

var_dump($_SERVER);

and figure it out that way. Handy way to remember if you forget the exact key name.

SeanDowney
+3  A: 
echo "<PRE>" . print_r($_SERVER, true) . "</PRE>";

Is useful because it prints out the data in a nice pre-formatted manner which is easy to read on the browser.

Rushi