Find a User's IP Address With a PHP Script

Users Can See Their IP Address With This PHP Script

Smiling businessman using laptop and leaning on desk in office
Martin Barraud/OJO Images/Getty Images

Retrieving the user's IP address is actually much simpler than you might think, and it can be done in a single line of PHP code.

What the PHP script you see below does is finds the IP address of a user and then posts the address on the page that holds the PHP code. In other words, any user who visits the page will be able to see their own IP address listed there.

Note: The way this PHP script is written here does not log any IP addresses nor does does it show a user anyone else's IP address - just their own.

The "What's My IP" PHP Script

To return the IP address of the person visiting your site, use this line:


To retrieve the user's IP address and then echo it's value back to the user, you can use this example:

 //Gets the IP address
 $ip = getenv("REMOTE_ADDR") ; 
 Echo "Your IP is " . $ip; 

Note: This is generally accurate but will not work as intended if the user is accessing your website behind a proxy. This is because the proxy's IP address will be shown instead of the user's true address.

How to Test That the IP Address is Correct

If you're not sure that the script is working, there are numerous websites you can visit to get some other perspectives on what your IP address is being reported as.

For example, after you implement the code from above, load the page and record the IP address that's given for your device. Then, go to or IP Chicken and see if the same IP address is shown there.

mla apa chicago
Your Citation
Bradley, Angela. "Find a User's IP Address With a PHP Script." ThoughtCo, Jan. 10, 2017, Bradley, Angela. (2017, January 10). Find a User's IP Address With a PHP Script. Retrieved from Bradley, Angela. "Find a User's IP Address With a PHP Script." ThoughtCo. (accessed February 22, 2018).