PHP Registering simple statistics


 

PHP Registering simple statistics

Learn how to PHP Registering simple statistics.

Learn how to PHP Registering simple statistics.

PHP Registering simple statistics

Have you ever wanted to know who visited a certain page? For fun? For hit monitoring? For security reasons? If you answered yes to any one of those questions. This tutorial is for you ! In this tutorial I will teach you how to make a simple PHP Script that writes to an external log file statistics about the Visitor, and the date and time that they visited !

This program follows a simple algorithm as follows:
1. Grab the needed variables such as: Year, Month, Day, Hour, and Minute.
2. Open the log file.
3. Write to the log file all of the information.

Ok first we will use the getdate() command to grab all of the needed arrays.

   $datearray = getdate();

Next we will define in the Year, Month, Day, Hour, and Minute variables.

   $year = $datearray["year"];
   $month = $datearray["mon"];
   $day = $datearray["mday"];
   $hour = $datearray["hours"];
   $minute = $datearray["minutes"];

Now we choose define where this information will be written to. I chose access.log.

   $filename = "access.log";

Then we create a variable that carries the information about the file to write to.

   $fl=fopen($filename,'a');

Lastly we write the information to the text file.

   fwrite($fl, "[$REMOTE_ADDR on $month $day , $year at $hour :$minute]\n\r");
   echo ".\n";

The final code:

<?
  $datearray = getdate();
  $year = $datearray["year"];
  $month = $datearray["mon"];
  $day = $datearray["mday"];
  $hour = $datearray["hours"];
  $minute = $datearray["minutes"];
  $filename = "access.log";
  $fl=fopen($filename,'a');
  fwrite ($fl, "[$REMOTE_ADDR on $month $day , $year at $hour :$minute]\n\r");
  echo ".\n";
?>

The output in the text file should appear as so:

[IP ADDRESS on MONTH DAY, YEAR at HOUR: MINUTE]

Heres what it will look like when someone accesses it:

[192.168.10.5 on 3 26 , 2009 at 12 :08]

Well this is the end of the tutorial!

Ads