Using PHP to Create Apache-Style Access.log
783 words | Posted by Scott on January 22nd, 2009
Scott was 23.33 years old when he wrote this!
Filed under: General, My Website, PHP, Python
THIS CODE HAS BEEN UPDATED! THIS CODE HAS BEEN UPDATED! THIS CODE HAS BEEN UPDATED! >>> CHECK OUT THE NEW CODE < << OBSOLETE CODE IS BELOW… |
My web server blocks access to my apache-generated visitor logs (commonly stored in “access.log”). Therefore, many great site usage stats generators (such as awstats – see this example) cannot be used to analyze web traffic to my site. (How many people go what pages? Where do they come from? What search phrases do they type into Google to find my website?) My web host does allow PHP, and access to php.ini, so I figured that I could generate my own access.log using PHP code. I succeeded, but had a hard time doing this because it’s not clearly documented elsewhere – so I’ll make it clear.
Sample line from access.log generated by my PHP script:
132.170.10.227 – - [22/Jan/2009:11:58:49 +0800] “GET /blog/2005-06-29-eva-05-attack-scotts-sanity/ HTTP/1.1″ 200 – “http://www.google.com/search?hl=en&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=8Lk&q=swharden+eva-05&btnG=Search” “Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5″
All I had to do was insert the following line at the end of my php.ini file:
auto_append_file = "/home/content/n/i/b/nibjb/html/logme.php"
And I placed logme.php in my root folder with the following code:
$logwriter_logformat = "combined"; // log format,combined or common
$logwriter_logdir = "/home/content/n/i/b/nibjb/html/logs/"; // physical path where your log file located
$logwriter_logfilename = "access.log"; // your log file's filename
$logwriter_timezone = "+0800"; // your server's time zone. +0800 means GMT+8
function logwriter_writelog($logstring){
global $logwriter_logdir,$logwriter_logfilename;
$fullpathfilename = $logwriter_logdir.$logwriter_logfilename;
if (!is_file($fullpathfilename)) {
print "Log file doesn't exist or file is corrupt.";
return;
}
if (!is_writeable($fullpathfilename)) {
print "Log file is not writable,please change its permission.";
return;
}
if($fp = @fopen($fullpathfilename, "a")) {
flock($fp, 2);
fputs($fp, $logstring);
fclose($fp);
}
}
function logwriter_handlevar($varname,$defaultvalue) {
$tempvar = getenv($varname);
if(!empty($tempvar)) {
return $tempvar;
} else {
return $defaultvalue;
}
}
if (!empty($REMOTE_HOST)) {
$logwriter_remote_vistor = $REMOTE_HOST;
}else{
$logwriter_remote_vistor = logwriter_handlevar("REMOTE_ADDR","-");
}
$logwriter_remote_ident = logwriter_handlevar("REMOTE_IDENT","-");
$logwriter_remote_user = logwriter_handlevar("REMOTE_USER","-");
$logwriter_date = date("d/M/Y:H:i:s");
$logwriter_server_port = logwriter_handlevar("SERVER_PORT","80");
if($logwriter_server_port!="80") {
$logwriter_server_port =
}else{
$logwriter_server_port = "";
}
$logwriter_request_method = logwriter_handlevar("REQUEST_METHOD","GET");
$logwriter_request_uri = logwriter_handlevar("REQUEST_URI","");
$logwriter_server_protocol = logwriter_handlevar("SERVER_PROTOCOL","HTTP/1.1");
if ($logwriter_logformat=="common") {
$logwriter_logstring = "$logwriter_remote_vistor $logwriter_remote_ident $logwriter_remote_user [$logwriter_date $logwriter_timezone] "$logwriter_request_method $logwriter_request_uri $logwriter_server_protocol" 200 -
";
}else{
$logwriter_http_referer = logwriter_handlevar("HTTP_REFERER","-");
$logwriter_http_user_agent = logwriter_handlevar("HTTP_USER_AGENT","");
$logwriter_logstring = "$logwriter_remote_vistor $logwriter_remote_ident $logwriter_remote_user [$logwriter_date $logwriter_timezone] "$logwriter_request_method $logwriter_request_uri $logwriter_server_protocol" 200 - "$logwriter_http_referer" "$logwriter_http_user_agent"
";
}
logwriter_writelog($logwriter_logstring);
Note that the PHP code must be surrounded with < ? php ?> as demonstrated here
The result? As you can tell, my logme.php dumps data to www.swharden.com/logs/access.log – if you browse a few pages on my website, or even use Google to search for me (ie: google for ’swharden’ and ‘minidisc’) you can see yourself in the logfile – pretty cool huh? Once I have a good volume of log data I’ll demonstrate how to turn it into useful information.
This entry was posted on Thursday, January 22nd, 2009 at 3:17 pmand is filed under General, My Website, PHP, Python. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.
2 Responses to “Using PHP to Create Apache-Style Access.log”
| Pharmacy Reviews wrote the following at 12:13:57 PM on October 22nd, 2009 |
|
Written at the begining of the year yet still very relevant, you should add your twitter link, ill follow for sure |
| My Review wrote the following at 11:18:01 AM on October 27th, 2009 |
|
More than that, I think it should be encouraged |