Analyzing AWStats logfiles with PHP
DescriptionAWStats is a open source tool to display logfiles of Apache server.
This code snippet allows you to read the AWSTats output files and use the extracted data for various purposes (e.g. for a visitor counter).
FunctionI the following we assume, that the AWStats files are stored in the folder
So, if the website is called www.cypax.net, in
For other folder names, you need to change the path in the code below!
AWStat will store the monthly logfiles in this folder in the form
awstats + MONTH + YEAR + . + URL + .txt. For example:
This code snippet determines from all logfiles the date and time of the last access and the total count of visits.
It works this way:
- Analyze every *.txt file and extract month and year from the filename.
- Read the file line-by-line.
- If the keyword
LastTime(last access) is found, the value will be saved in a variable. With the keyword
TotalUniqueit determines the number of visits and adds this to a variable
When finished, the timespamp of the last access is stored in
$lastvisitand the number of total visits in
CodeAnd now the corresponding code:
TotalUniqueis the number of "seen" visits from different unique visitors.
This doesn't include visits from robots and search engines (Google & Co.)!
If you also like to count this visits, you need to replace
Because the code doesn't extract any more information after TotalUnique from the file there is a
Of course you can also extract further data from the file - if you do so, you just need to place the
break;after the last keyword...
$lastvisit(format YYYYMMDDHHNNSS, eg. 20140325000755) can be decoded with: Then, you can re-construct it with
echo $year.'-'.$month.'-'.$day.' at '.$hour.':'.$minute.':'.$sec;
to 2014-03-25 at 00:07:55.