Thursday, June 23, 2016

Creating a file based log system

If we have a website we would certainly want to know the impressions of our website visitors. 

In addition to knowing about information of visitors, a log file is also useful to help analyse the security.

I will go directly to explain the log system file in PHP script: 

<?php 
// checking the bowsers type 
$agent = $_SERVER [HTTP_USER_AGENT]; 

// * checking where the script is executed from -- GET (URL) 
$uri = $_SERVER [REQUEST_URI]; 

// * checking visitors IP 
$ip = $_SERVER [REMOTE_ADDR]; 

/ * checking where the script is referred from 
$ref = $_SERVER [HTTP_REFERER]; 

// * checking visitors proxy
$original = $_SERVER [HTTP_X_FORWARDED_FOR]; 

// * checking visitors connection 
$via = $_SERVER [HTTP_VIA]; 

// * variable date 
$dtime = date (r); 

// * note if  visitors use transparent Proxy 
// * Then the $_SERVER [HTTP_X_FORWARDED_FOR] will display the visitors IP 
// * otherwise $_SERVER [ REMOTE_ADDR] will display the Proxy 
// * For clear about Proxy i will explain in another tutorial 

// * This is a description of variable entry_line: 
$entry_line = "Time: $dtime | Original IP: $ip | Browser: $agent | URL: $uri | referrer: $ref | Proxy: $original | Connection: $via 
";   // * <- Attention! This must be a new line or you press enter to create a new line 

/ * "fopen ()" function for opening files, "a" is the most important.!, 
/ * This works if the file "trace.txt" does not exist in the server then PHP will create 
$fp = fopen ( "trace.txt", "a"); 

/ /* "fputs ()" function for writing into the log file 
fputs ($fp, $entry_line); 

// * "fclose ()" function to close the file 
fclose ($fp); 

?> 

This file based log system using php function is pretty useful instead of using mysql connection