Analyzing AWStats logfiles with PHP
Description
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).
Function
I the following we assume, that the AWStats files are stored in the folder./logs
in htdocs.
So, if the website is called www.cypax.net, in
http://www.cypax.net/logs/
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: awstats052006.cypax.net.txt
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 keywordTotalUnique
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
.
Code
And now the corresponding code:Notes
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:
Then, you can re-construct it with
echo $year.'-'.$month.'-'.$day.' at '.$hour.':'.$minute.':'.$sec;
to 2014-03-25 at 00:07:55.