Every guy with a webpage eventually gets really curious about who is reading his website and whether he's doing anything worthwhile or if he's just entertaining himself. Serious webmasters will ensure they get server logs so they can analyze their traffic thoroughly. The web package that I purchased through my host gives me some standard web site statistics (produced by Report Magic) and these reports are great as summary information. But if I want to look at my actual server logs I'm out of luck unless I want to pay an extra couple bucks a month for them.
Ok, so I'm cheap. Instead of doing that, I thought I'd brush off my PHP manual and figure out how to do it myself. It wasn't that hard to do. Consider that it only took a couple hours time, I wasn't testing it locally (had to FTP every time), and my PHP knowledge is maybe just a notch above novice (though my familiarity with C/C++ certainly helps).
For me, PHP ranks up there with Java in terms of free software that allows me to get something accomplished quickly, it's got great online documentation, a wide user base and a dizzying amount of library support. If you're interested in learning more, I also recommend this book from Amazon.
Anyway, the file that I wrote that does server logging looks like this:
mylogger.php:
<?php
$_ = " ";
$log_string = $_SERVER[REMOTE_ADDR]
. $_ . $_SERVER[HTTP_X_FORWARDED_FOR]
. $_ . "[" . date("Y-m-d H:i:s O") . "]"
. $_ . $_SERVER[REQUEST_METHOD] . $_ . $_SERVER[REQUEST_URI] . $_ . $_SERVER[SERVER_PROTOCOL]
. $_ . "?"
. $_ . $_SERVER[PATH_TRANSLATED]
. $_ . filesize($_SERVER[PATH_TRANSLATED])
. $_ . $_SERVER[HTTP_REFERER]
. $_ . $_SERVER[HTTP_USER_AGENT]
;
$f = fopen("full_path_to_log_files/log_" . date("Y-m-d") . ".txt",'a');
fwrite($f, $log_string);
fwrite($f, "rn");
fclose($f);
?>
This simple little PHP nugget simply takes all the global environment variables and formats them into a suitable line for my server log. It then opens the log file and appends the line to the server log. The server log is named for each day.
One little caveat is that since the PHP is only processed once the file is fetched, there is no way to return the web server status/error code (like 200, 404) since it's too late, the file was found and fetched. Oh well, I just put a ? in this field as I'm really only interested in what people are browsing and where they're coming from.
Of course you'll have to fill in your own "full_path_to_log_files" but you get the idea.
Now the next step is to include this code in all my web pages. It might sound daunting but it really was quite simple because I had already used PHP to break up my web page into common header/footer/menu sections so it was the simple modification of one of those files to add the single line:
include("mylogger.php");
I also had to do a similar modification to my phpBB files (common.php) and my WordPress files (wp-blog-header.php).
As a bonus, if you want to get fancy, you can make a front-end to view all your logs. Something like this should do:
<?php
print "<HTML><BODY>";
if ($dir = opendir('full_path_to_log_files')) {
echo "<h3>CodeDread Server Logs:</h3>";
while (false !== ($file = readdir($dir))) {
if( strncmp($file, "logs_", 12) == 0 ) {
printf("<BR><A HREF="url_to_your_log_file_directory/%s">%s</A></BR>", $file, substr($file, 12, 10) );
}
}
closedir($dir);
}
print "</BODY></HTML>";
?>
Now you can browse to this page and see a list of all your server logs, click on each one to view it. If you want to get really fancy you'd write a third PHP file that would format the server logs into HTML so you could check out who owns each IP address, but only an insane person would do that...
[…] link here. The example given would suit a PHP based blog package like WordPress, or it could be adapted to whatever programming language is your chocie (the environment variables used have equivalents in most script languages). The principal is that you write a little piece of code (PHP /Perl / Ruby / etc) that simply takes all the global environment variables and formats them into a suitable line for the server log, then opens the log file and appends the line to the server log. The server log is named for each day. The next step is to include this code in all your web pages. It’s easier if you do what I did with the site I built for Suzanne Kennedy which already uses PHP to break up the pages into common header/footer/menu sections. This makes it a simple modification of one of those files to add an include to the logger.php script. Bonus 1 – if you want to get fancy, you can make a front-end to view all your logs. Now you can browse to this page and see a list of all your server logs, click on each one to view it. Bonus 2 – If you want to get really fancy you’d write a third PHP file that would format the server logs into HTML so you could check out who owns each IP address. Bonus 3 – by updating the .htaccess file you can redirect 404 errors to your own PHP document that will display the 404 error and also log when misdirections occur at my site (since the requested document is in the GET request). […]