|
There are many different traffic analysis tools, ranging fromsimple counters to complete traffic analyzers. Although thereare some free ones, most of them come with a price tag. Why notdo it yourself? With PHP, you can easily create a log filewithin minutes. In this article I will show you how!
Getting the information The most important partis getting the information from your visitor. Thankfully, thisis extremely easy to do in PHP (or any other scripting languagefor that matter). PHP has a special global variable called$_SERVER which contains several environment variables, includinginformation about your visitor. To get all the information youwant, simply use the following code: // Getting theinformation $ipaddress = $_SERVER['REMOTE_ADDR']; $page ="http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}"; $page .=iif(!empty($_SERVER['QUERY_STRING']),"?{$_SERVER['QUERY_STRING']}", ""); $referrer =$_SERVER['HTTP_REFERER']; $datetime = mktime(); $useragent =$_SERVER['HTTP_USER_AGENT']; $remotehost =@getHostByAddr($ipaddress); As you can see the majorityof information comes from the $_SERVER variable. The mktime()(http://nl2.php.net/mktime) and getHostByAddr()(http://nl2.php.net/manual/en/function.gethostbyaddr.php)functions are used to get additional information about thevisitor.
Note: I used a function in the above example called iif(). Youcan get this function at http://www.phpit.net/code/iif-function.
Logging the information Now that you have allthe information you need, it must be written to a log file soyou can later look at it, and create useful graphs and charts.To do this you need a few simple PHP function, like fopen(http://www.php.net/fopen) and fwrite(http://www.php.net/fwrite).
The below code will first create a complete line out of all theinformation. Then it will open the log file in "Append" mode,and if it doesn't exist yet, create it.
If no errors have occurred, it will write the new logline to thelog file, at the bottom, and finally close the log file again. // Create log line $logline = $ipaddress . '|' . $referrer. '|' . $datetime . '|' . $useragent . '|' . $remotehost . '|' .$page . " ";
// Write to log file: $logfile ='/some/path/to/your/logfile.txt';
// Open the log file in "Append" mode if (!$handle =fopen($logfile, 'a+')) { die("Failed to open log file"); }
// Write $logline to our logfile. if (fwrite($handle, $logline)=== FALSE) { die("Failed to write to log file"); }fclose($handle); Now you've got a fully function loggingmodule. To start tracking visitors on your website simplyinclude the logging module into your pages with the include()function (http://www.php.net/include): include('log.php'); Okay, now I want to view my logfile After a while you'll probably want to view yourlog file. You can easily do so by simply using a standard texteditor (like Notepad on Windows) to open the log file, but thisis far from desired, because it's in a hard-to-read format.
Let's use PHP to generate useful overviews for is. The firstthing that needs to be done is get the contents from the logfile in a variable, like so: // Open log file $logfile ="G:projectsphpitcontentrack your visitors using phplog.txt";
if (file_exists($logfile)) {
$handle = fopen($logfile, "r"); $log = fread($handle,filesize($logfile)); fclose($handle); } else { die ("The logfile doesn't exist!"); } Now that the log file is in avariable, it's best if each logline is in a separate variable.We can do this using the explode() function(http://www.php.net/explode), like so: // Seperate eachlogline $log = explode(" ", trim($log)); After that itmay be useful to get each part of each logline in a separatevariable. This can be done by looping through each logline, andusing explode again: // Seperate each part in each loglinefor ($i = 0; $i < count($log); $i++) { $log[$i] =trim($log[$i]); $log[$i] = explode('|', $log[$i]); }
Now the complete log file has been parsed, and we're ready tostart generating some interesting stuff.
The first thing that is very easy to do is getting the number ofpageviews. Simply use count() (http://www.phpit.net/count) onthe $log array, and there you have it; echo count($log) ." people have visited this website."; You can alsogenerate a complete overview of your log file, using a simpleforeach loop and tables. For example: // Show a table ofthe logfile echo '<table>'; echo '<th>IPAddress</th>'; echo '<th>Referrer</th>'; echo'<th>Date</th>'; echo'<th>Useragent</th>'; echo '<th>RemoteHost</th>';
foreach ($log as $logline) { echo '<tr>';
echo '<td>' . $logline['0'] . '</td>'; echo'<td>' . urldecode($logline['1']) . '</td>'; echo'<td>' . date('d/m/Y', $logline['2']) . '</td>';echo '<td>' . $logline['3'] . '</td>'; echo'<td>' . $logline['4'] . '</td>';
echo '</tr>';
}
echo '</table>';
You can also use custom functions to filter out search enginesand crawlers. Or create graphs using PHP/SWF Charts(http://www.maani.us/charts/index.php). The possibilities areendless, and you can do all kinds of things!
In Conclusion... In this article I have shownyou have to create a logging module for your own PHP website,using nothing more than PHP and its built-in functions. To viewthe log file you need to parse it using PHP, and then display itin whatever way you like. It is up to you to create a kick-asstraffic analyzer.
If you still prefer to use a pre-built traffic analyzer, have alook at http://www.hotscripts.com.
|
|