Analyzing AWStats logfiles with PHP


AWStats 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).


I the following we assume, that the AWStats files are stored in the folder ./logs in htdocs.
So, if the website is called, 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:
  1. Analyze every *.txt file and extract month and year from the filename.
  2. Read the file line-by-line.
  3. If the keyword LastTime (last access) is found, the value will be saved in a variable. With the keyword TotalUnique it determines the number of visits and adds this to a variable $totalvisits.

When finished, the timespamp of the last access is stored in $lastvisit and the number of total visits in $totalvisits.


And now the corresponding code:
PHP code:


TotalUnique is 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 TotalUnique by TotalVisits.

Because the code doesn't extract any more information after TotalUnique from the file there is a break; after $visits=$buffer[1];.
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...

The timestamp $lastvisit (format YYYYMMDDHHNNSS, eg. 20140325000755) can be decoded with:
PHP code:
Then, you can re-construct it with
echo $year.'-'.$month.'-'.$day.' at '.$hour.':'.$minute.':'.$sec;
to 2014-03-25 at 00:07:55.