Tech Tips

How to Acquire the Address of People on Servers

by Kyle Cavnett, Demand Media

A visitor to your web server automatically broadcasts a number of useful pieces of information, including geographic location, operating system, browser and IP address. A PHP script can be used on any web page to acquire the IP address of people connecting to your server. The IP address will be stored as a variable that can be used for a variety of purposes. Alternatively, you can simply save the IP address to a file to examine later. A bit of familiarity with the PHP coding language will help you, but you can also implement this code on your website even if you have never used PHP before.

Step 1

Type the command " tags of an HTML page. This notifies the browser that the following lines will be PHP code.

Step 2

Enter the line "$ip=@$REMOTE_ADDR;" (without the quotes). This tells the server to store the IP address as the variable $ip. You can rename $ip to any other name you choose. If your web hosting provider has the global register set to off, the script will not work. Replace this line with "$ip=$_SERVER['REMOTE_ADDR'];" instead. The easiest way to tell is by running the web page with the script and changing it if necessary.

Step 3

Type "$iplist="iplist.txt";" (without the opening and closing quotes). This will define a text file, iplist.txt, as the storage file of your acquired IP addresses.

Step 4

Type "$fh = fopen($iplist, 'w') or die("can't open file");" (without the opening and closing quotes). This will make your script open the text file so that information can be written into it by the server.

Step 5

Enter the line "fwrite($fh, $ip);" (without the quotes) to write the IP address you acquired to the iplist.txt file.

Step 6

Type "fclose($fh);" (without the quotes) to close the file.

Step 7

Enter "?>" (without the quotes) to end the PHP script. This will tell the browser to go back to interpreting the rest of the page as basic HTML. Any time a visitor visits that webpage, his IP address will now automatically be stored in the iplist.txt file.

Tips

  • The $ip variable can also be used in any other PHP script on the page if you have another use for the IP address.

About the Author

Kyle Cavnett is a legal and political commentator whose work has appeared in law and philosophy journals and online in legal blogs and article repositories. He has been a writer since 2008. He holds a Bachelor of Science in psychology from University of California, San Diego and a Juris Doctor from Lewis and Clark School of Law.

TopLeft