11:57:39 pm on 9/6/10
Menu
» Home
» About Scott
» VD Labs
» QRSS VD
» Old Stuff
» Archive
» Contact

Categories
» C/C++
» Circuitry
» DIY ECG
» General
» Linux
» Microcontrollers
» Molecular Biology
» My Website
» PHP
» Prime Numbers
» Python
» Radio
» UCF Lab
» Everything
Writings
» MD Labels
» Streamrip
» AIM Thoughts
» WindowsXP?
» Partitioning
» CD/DVD Repair
» Monitor Info
» CRT Deflection
» Venomcrack
» Flash Thing
» Heart/Brain
» Diabetes
» Triops
» Biomed

Friends
» Fred
» Kyle W
» Nick
» Louis
» Tom
» Kyle H




Archives
» September 2010
» August 2010
» July 2010
» June 2010
» May 2010
» April 2010
» March 2010
» February 2010
» January 2010
» December 2009
» September 2009
» August 2009
» July 2009
» June 2009
» May 2009
» April 2009
» March 2009
» February 2009
» January 2009
» December 2008
» November 2008
» October 2008
» September 2008
» September 2007
» December 2006
» August 2006
» January 2006
» August 2005
» July 2005
» June 2005
» May 2005
» April 2005
» March 2005
» February 2005
» January 2005
» December 2004
» November 2004
» October 2004
» September 2004
» August 2004
» July 2004
» June 2004
» May 2004
» April 2004
» March 2004
» February 2004
» January 2004
» December 2003
» November 2003
» October 2003
» September 2003
» August 2003
» July 2003
» June 2003
» May 2003
» April 2003
» March 2003
» February 2003
» January 2003
» December 2002
» November 2002
» October 2002
» September 2002
» June 2001
« Signal Filtering with Python
I’m in Love with InkScape »


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 < <<
[Generate Apache-Style HTTP Access Logs via SQL and PHP]

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

Leave a Reply




copyright © 2006 swharden@gmail.com