Save the IP addresses of visitors to your site

December 2016

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.

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

This will open the index.php page for writing.

Example:

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

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

$_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.

Related :

This document entitled « Save the IP addresses of visitors to your site » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.