Save the IP addresses of visitors to your site

Ask a question
Saving the IP addresses of the visitors to a website can prove useful in case of an online hacking attack or for statistical purposes. A small PHP code or script can help webmasters to record the IP addresses of the visitors along with the data and time stamp of the visit. The $_SERVER["REMOTE_ADDR"] is the environment variable that holds the IP address of a visitor. The $_SERVER["REMOTE_ADDR"] can be used to obtain the IP address of the visitor and record it in a separate document. File operators such as fopen(),fputs() and fclose() can accomplish the task of saving the IP addresses of visitors to a website.

Record the IP address of visitors to your site

If you are a victim of an attack or an attempted hack, it may be useful to know who connected to the pages of your site at specific times. How does one save these addresses?

You will need to open/write a page on your site and write these addresses between the tags
?> <php and commentary.

$file = fopen("index.php", "r+");   

This will open the index.php page for writing.


If you define the date and time with the "date ()", here is the code:

$file = fopen("index.php", "r+");   
fputs($file, "<php //$_SERVER["REMOTE_ADDR"] connected $numday $month $year at $hour h $minutes ?>\n");   

$_SERVER["REMOTE_ADDR"] is an environment variable that contains the IP address of the visitor.

With this script, each IP connecting to your site will be saved.
Jean-François Pillou

CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jeff Pillou, founder of CCM reaches more than 50 million unique visitors per month and is available in 11 languages.

Learn more about the CCM team