In an earlier post I made, Lighttpd Access Log Parsing, I wrote a quick Bash line to parse Lighttpd’s access log file that you can easily port over to any other log file. This time, I’ve done the same thing, but with PHP (using HTML for output, but you can change that if you like). Click continue to see the awesome powers PHP can have with some regex magic, and basic HTML!
Going to start this off with the script, and then explain any of the finer details that I feel should be addressed. As the script doesn’t correctly show up in this blog, I’ve uploaded it to pastebin (due to the HTML, it conflicts with the PHP coding as well): http://pastebin.com/82fkhrjX
Since this is a basic version of a log analyzer, it doesn’t have all the bells and whistles, but that’s why I started work on the “parser()” function, which already handles IP addresses.
There’s not a whole lot that needs to be actually addressed, as it uses all regular PHP functions without the need for external libraries (minus libpcre for regex). The regex does the same thing as the Bash line does in the previous article, but makes it easier to manage as each line will be the first array. Originally I was going to write this as a command line script, but I decided to make it HTML instead, which is why some of the variables aren’t actually used, even though I set them (which also just makes it easier in the end if you do want a CLI version).
All you need to do is save it to your computer/server, give it executable rights (and change the shabang to the correct php-cli path if needed), and run it…as well as editing the variables $file and $data_path.