Search 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 it show a user anyone else's IP address - just their own.

"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:

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.

Test the IP Address

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. or IP Chicken are good places to check to see if the same IP address is recorded there.