Translate

Thursday, June 7, 2018

Find a User's IP Address With a PHP Script Users Can See Their IP Address With This PHP Script

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:
Getenv("REMOTE_ADDR")
To retrieve the user's IP address and then echo it's value back to the user, you can use this example:
<?php
//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 WhatsMyIP.org or IP Chicken and see if the same IP address is shown there.

No comments:

Post a Comment