How to determine or retrieve visitor’s IP address using PHP
Q. How do I determine or retrieve visitor's IP address using PHP server side programming?
A. Use following enviorment variable to get visitor's IP address:
No Proxy detection
=> REMOTE_ADDR - Remote client IP address
With Proxy detection
=> HTTP_X_FORWARDED_FOR - Get Proxy server IP
PHP getenv() function
=> Use php getenv() function to read the value of the environment.
Here is PHP code (download source code link):
<html> <head> <title>What is my IP address?</title> </head> <body> <?php if (getenv(HTTP_X_FORWARDED_FOR)) { $pipaddress = getenv(HTTP_X_FORWARDED_FOR); $ipaddress = getenv(REMOTE_ADDR); echo "Your Proxy IPaddress is : ".$pipaddress. "(via $ipaddress)" ; } else { $ipaddress = getenv(REMOTE_ADDR); echo "Your IP address is : $ipaddress"; } ?> </body> </html>
Try the demo here
Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
Related Other Helpful FAQs:
- How to test or check reverse DNS
- How you can run a c program in Linux?
- Determine / Find ethernet connection speed
- How to compile program under Linux / UNIX / FreeBSD
- How do I find the url for my cgi-bin?
Discussion on This FAQ
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: client ip address, client_ip_address, environment, php find my ip, php getenv, php server, php_server_side, proxy server, proxy_server, remote proxy, REMOTE_ADDR, server ip, server_ip, server_side_programming



October 17th, 2006 at 3:55 am
it is working on localhost
but it doesn’t work after uploading it on website
i m getting blank page..
February 6th, 2007 at 7:42 am
I tried with the given php code “to determine or retrieve visitors ip address in PHP” in two different machines and got the same ip address.
July 3rd, 2007 at 11:07 am
I want to get visitor IP address into table of my database.
please help me
thanks
July 15th, 2007 at 2:53 pm
i am getting a blank page after i paste the code onto my website. plz help.
August 9th, 2007 at 4:57 pm
I never got it to work. The host always displays an error about an unexpected & when expecting ;
But here is a better way, just remember to use the as needed:
http://www.plus2net.com/php_tutorial/php_ip.php
October 11th, 2007 at 8:30 pm
Excellent tutorial!
It works for me. I havent uploaded it to my site yet. But it looks find and i guess it should work.
Thanks
November 9th, 2007 at 10:14 pm
In line 12 and 15 delete and rewrite the ” and it will work.
November 24th, 2007 at 1:14 am
very nicely given. thanx a tone
March 23rd, 2008 at 8:12 pm
The tutorial is really great it solved my problem at once…On my local machine and on my webserver it worked.
Thanx,
Umair
June 13th, 2008 (4 weeks ago) at 11:44 pm
Hi All
This code worked for me fine……
Thanks to the programmer……
And For this
# Marvin Henry Says:
February 6th, 2007 at 7:42 am
I tried with the given php code “to determine or retrieve visitors ip address in PHP” in two different machines and got the same ip address.
Reply
I think u get a Ip address from the DHCP server.
In This case You should the ip address of the Server and not of the client Machine.
Anyways this solved my problem….
Thanks a ton.
Deepak Poolamadai