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
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- My 10 UNIX Command Line Mistakes
- Linux: 20 Iptables Examples For New SysAdmins

- 25 PHP Security Best Practices For Sys Admins
- The Novice Guide To Buying A Linux Laptop
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
Facebook it - Tweet it - Print it -


{ 21 comments… read them below or add one }
it is working on localhost
but it doesn’t work after uploading it on website
i m getting blank page..
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.
I want to get visitor IP address into table of my database.
please help me
thanks
i am getting a blank page after i paste the code onto my website. plz help.
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
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 :)
In line 12 and 15 delete and rewrite the ” and it will work.
very nicely given. thanx a tone
The tutorial is really great it solved my problem at once…On my local machine and on my webserver it worked.
Thanx,
Umair
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
nice work :)..
But how will you know if the user is using a socks server?
When putting this code into a page, make sure has the “.php” extension, and not “.html” or “.htm”
Help me .I want try connect to computer in php programing .so please some example progarm and explain me
Thank You
Help me .I want try connect two computer in php programing using socket programing.so please some example progarm and explain me
Excellent one! Thanks for uploading…
Great … Thanks for the post .. I was thinking about how to determine the country from this ip address ?
Good code. It’s useful. Thanks for providing that one.
Hello evry1….I wanna 9 the ip address of local machine in the form of 192.168…….but i m getting the server’s ip address……. so can any1 give me solution of dis problm
THANKS IN ADVANCE
I am PHP web developer & CEO of web development company. I have excluded my office’s IP address from Google analytics.
But, I am little bit confuse with data of Google analytics which still give me details regarding my office staff member’s visit.
Can I make some integration to solve this issue? I want to check IP address of all visitors who are visiting my website in specific time frame.
Can you give me some idea about it or not? May I do it with help of Google analytics?
Hold on. You said it can get the IP address of someone using a proxy. NOT TRUE. I’ve just tested this on a variety of proxies, and it didn’t stop it. Totally useless extra line of code.
I am enhancing my website with php code I have the following code that will display a message to the suspect of an sql injection what I would like to do is add some code to log the visitors IP address to a table called sql_injection_suspects where it will store the IP address and the time of the event and the value of the save string:
$PUserid=strip_tags($_POST['lUSER']); $Ppassword=Strip_tags($_POST['lPASSWORD']); $sqlSafeUserid=mysqli_real_escape_string($con, $_POST['lUSER']); //Query users table and Email table to see if either exist $queryUserid = mysqli_query($con,"SELECT UserID, Password FROM tt_users WHERE UserID='$sqlSafeUserid'"); $rowUserid = mysqli_num_rows($queryUserid); if (strpos($sqlSafeUserid, '\\') !== false) { echo 'your attempt to inject sql into my database has been reported we have modified your input to: ' .$sqlSafeUserid .''; echo '\'s and "s are not permited in feilds that query databases and will be prefixed with a \\'; echo 'Click here to leave my website with your head hanging in shame'; } else {// Some code to find the userid and validate the userid and password }