2 simple tools for log data analysis

Simple Log Writer

The Log Writer is a small script that you should include in the beginning or end of each page like this:
<? include ("/home/www/htdocs/yoursite/inc_log.php");?>
To learn your own host, first leave it as it is in my example, then visit your page a few times in succession and look at the logfile, where you'll see your own accesses. Configure 2 parameters: a writable path on your server and on your own host. The second parameter is optional.
If you don't know your own host, leave it as is.


The logging directory must be writable for scripts.
Tip: this can be a directory with session data.

The difference to server logfiles is:

  1. Only successfull requests will be included into logs. The server logs contain failed requests (answer code 400-500) as well.
  2. Only requests for pages of your choice will be included. You can exclude non-informative requests like subframes, pop-ups etc. right from the beginning.
The files are created monthly. When the month is out, the file is archived into a gzip-file and a new file is created.

Script source (available in the download package):

<?
/*** Config area ***/
// a writable directory at your site (it may be not easy to find 
if your provider uses SAFE MODE)
$writepath="/home/www/htdocs/yoursite/writableDirectory/logging/";

// if you would like to exclude your own accesses, mark up an 
essential part of your hostname to distinguish it from others 
or leave it as is

$own_host="myID.myProvider.com";
/*** end of config area ***/

$filename=$writepath.date("m")."_".date("Y").".txt";

if (!file_exists($filename)) { 
	//new month => gzip the last log
	/*** cut this section out if your provider doesn't support 
	gzip functions ***/	
	$lastmonth=date("n")-1; $lastyear=date("Y");
	$lastmonth=str_pad($lastmonth,2,"0",STR_PAD_LEFT);
	if (date("n")=="1") {
		$lastmonth=12;$lastyear=date("Y")-1;
		$lastlogfile=$writepath.$lastmonth."_".$lastyear.".txt";
		if (file_exists($lastlogfile)) {
			$dest_file=$lastlogfile.".gz";
			$data = implode("", file($lastlogfile));
   			$gz_data = @gzencode($data);
   			$fp = fopen($dest_file, "w+");
   			fwrite($fp, $gz_data);
   			fclose($fp);
   			unlink($lastlogfile);
		}
	}
	/*** end of cut ***/
	
	$myfile=fopen($filename,"w"); fclose($myfile); 
}

// select the "m/d/Y" if you prefer
$dateformat="d/m/Y";

$dat=date($dateformat);
$tim=date("H:i:s");
$ip=$_SERVER["REMOTE_ADDR"];
@$host=gethostbyaddr($ip); 
if (empty($host)) $host=$ip;
$browser=$_SERVER["HTTP_USER_AGENT"];
$referer=$_SERVER["HTTP_REFERER"];
if (empty($referer)) $referer="-";
$method=$_SERVER["REQUEST_METHOD"];
$page=$_SERVER['REQUEST_URI'];

if (!eregi($own_host,$host)) {
	$fil=fopen($filename,"a");
	fwrite($fil,$ip." ".$host." [".$dat." ".$tim."] \""
	.$method." ".	$page."\" ".$referer." \"".$browser."\"\r\n");
	fclose($fil);
}
?>