31
Anyone who owns a website knows that stats are important. If you’re like me you like to know how many visitors your site is getting and what kind of bandwidth your using. With AWStats you can get comprehensive reports of your user statistics but I don’t want to have to go through my Control Panel and open AWStats everyday to see how many visitors my site has gotten. To make it easier I created this little script to automatically grab my AWStats data and display it in a neat and orderly way.
First off, here is the complete code in it’s entirety. You can copy and paste this code, change some settings and your good to go, I’ll explain the what the code does next.
<?php
$open = file_get_contents(”http://CPANEL_USERNAME:CPANEL_PASSWORD@www.YOURDOMAIN:2082/awstats.pl?config=techwhiz.info&ssl=〈=en&framename=mainright”);
$regex_1 = ‘/<span style=”font-size: 12px”>(.*)<\/span>/’;
$regex_2 = ‘/<b>(.*)<\/b>
/’;
preg_match_all($regex_1, $open, $result);
preg_match_all($regex_2, $open, $result2);$month = substr($result[0][0], 34, -20);
$day = substr($result[0][0], 31, -24);
$year = substr($result[0][0], 37, -15);
$time = substr($result[0][0], 44, -7);$uniques = substr($result2[0][0], 3, -198);
$visitors = substr($result2[0][0], 35, -166);
$hits = substr($result2[0][0], 136, -64);$date = “$month | $day | $year | $time”;
$file = fopen(”awstats.txt”, “r”);
$old_date = fread($file, filesize(”awstats.txt”));if ($old_date != $date) {
$file2 = fopen(”awstats.txt”, “r+”);
fwrite($file2, $date);
fclose($file2);
$updated = “1″;
}fclose($file);
if ($updated == “1″) {
Print “
<span class=\”updated\”>Updated!</span>
“;
unset($updated);
}Print “
Last Update: $month $day, $year - $time
<table cellpadding=\”5\”>
<tr>
<td>Unique Visitors: </td><td>$uniques</td>
<td>Total Visitors: </td><td>$visitors</td>
<td>Total Hits: </td><td>$hits</td>
“;?>
This script will generate the following output:
This is an example of the output. I just put it on my Desktop using an Active Desktop object.
Now, I will explain the script and how it works:
$open = file_get_contents(”http://CPANEL_USERNAME:CPANEL_PASSWORD@www.YOURDOMAIN:2082/awstats.pl?config=techwhiz.info&ssl=〈=en&framename=mainright”);
$regex_1 = ‘/(.*)<\/span>/’;
$regex_2 = ‘/(.*)<\/b>
/’;
preg_match_all($regex_1, $open, $result);
preg_match_all($regex_2, $open, $result2);
This code grabs the awstats main page. Note that you will need to insert the correct data where it says CPANEL_USERNAMe and CPANEL_PASSWORD, but remember to keep the “:” between them. Also replace YOURDOMAIN with your domain you use for your cPanel access.
The $regex_1 and $regex_2 are used to find the “last updated” date and the unique visitors, total hits, etc that we will use later. the preg_match grabs the matching results and puts them into an array for easy access.
$month = substr($result[0][0], 34, -20);
$day = substr($result[0][0], 31, -24);
$year = substr($result[0][0], 37, -15);
$time = substr($result[0][0], 44, -7);
This code basically separates the different parts that make up the date, this is used multiple times for both displaying the date and saving it as a formatted string in a text file to compare with future versions of the fetched page to determine if it has been updated.
$uniques = substr($result2[0][0], 3, -198);
$visitors = substr($result2[0][0], 35, -166);
$hits = substr($result2[0][0], 136, -64);
This code is very similar to the code above, however instead of breaking up (substr) $result it breaks up $result2. The code here pulls out the Unique Visitors, Total Visitors, and Total Hits. It is very easy to expand this code to pull the other stats available through AWStats such as Bandwidth, etc.
$date = “$month | $day | $year | $time”;
$file = fopen(”awstats.txt”, “r”);
$old_date = fread($file, filesize(”awstats.txt”));if ($old_date != $date) {
$file2 = fopen(”awstats.txt”, “r+”);
fwrite($file2, $date);
fclose($file2);
$updated = “1″;
}fclose($file);
This section of code formats the date to be put into the text file, then opens the file “awstats.txt”. Next it determines if the “Last Updated” date is the same as the “old date” stored in the text file are the same, if so then it doesn’t need to update it, else it stores the current date in the text file and sets “$updated=1″ which tells the script the stats have been updated. At the end it closes the file.
if ($updated == “1″) {
Print “<span class=\”updated\”>Updated!</span>
“;
unset($updated);
}
This piece of code determines if the stats have been updated by reading “$updated” if it equals “1″ then it has been updated, anything else and it has not. If it has been updated then it prints “Updated!” to the user (output) and then unsets the “$updated” variable to prepare for the next go around.
Print “
Last Update: $month $day, $year - $time
<table cellpadding=\”5\”>
<tr>
<td>Unique Visitors: </td><td>$uniques</td>
<td>Total Visitors: </td><td>$visitors</td>
<td>Total Hits: </td><td>$hits</td>“;
?>
This code prints the stats in a neat orderly table as shown in the picture at the beginning of this article. Then closes off the body and html tags and everything is done!
I know this has been a very long tutorial but I hope it was worth it! It is definitely a useful script and can be easily modified and used in other scripts or for anything really!
If you see anything wrong or any mistakes or just want to leave a comment by all means please do, just leave a comment below!

Good tutorial man.Love it
Great tutorial
Getting an error:
Parse error: syntax error, unexpected ‘:’ in C:\wamp\www\stats.php on line 3
=\
Great tutorial though, Please help
@Colin - If you strictly copy and paste the code you are going to have to replace all the ‘ and ” because of the way my site renders it in Unicode, therefore you will have to do a simple “search and replace” (Ctrl+H in notepad) to fix your problem, then it should work no problem!
er, why are you trying to steal our passwords?:
”
…RD@www.YOURDOMAIN:2082/awstats.pl?config=techwhiz.info&ssl=〈=en&framename=mainright”);
”
just caus its hidden off the word-wrap doesnt mean we cant see
@pleh
He isnt stealing passwords thats simply an example you replace techwhiz.info with your domain
Automatically Fetch Data From AWStats | TechWhiz.info…
Anyone who owns a website knows that stats are important. If you’re like me you like to know how many visitors your site is getting and what kind of bandwidth…
STUMBLED!
I love these kinds of tutorials, thanks for sharing.
VOTED for this post at:
http://www.newsdots.com/tutorials/automatically-fetch-data-from-awstats-techwhiz-info/
Sweet script dude. Congrats and keep up the good work.
Great script, I was looking for a way to get those stats!
Also how would you get the country based stats too?
I plan to display the stats overlayed on a map(i know how to do this part, atleast).
Thanks
I got the following error. I copied the code and inserted my cpanel info, uploaded it and i got the following error.. what’s these characters in chinese ?? I want everything in english.. help me
(.*)⼀✀㬀� preg_match_all($regex_1, $open, $result);瀀爀攀最开洀愀琀挀栀开愀氀氀⠀␀爀攀最攀砀开㈀Ⰰ ␀漀瀀攀渀Ⰰ ␀爀攀猀甀氀琀㈀⤀㬀� ␀洀漀渀琀栀 㴀 猀甀戀猀琀爀⠀␀爀攀猀甀氀琀嬀 崀嬀 崀Ⰰ ㌀㐀Ⰰ ⴀ㈀ ⤀㬀� $day = substr($result[0][0], 31, -24);␀礀攀愀爀 㴀 猀甀戀猀琀爀⠀␀爀攀猀甀氀琀嬀 崀嬀 崀Ⰰ ㌀㜀Ⰰ ⴀ㔀⤀㬀� $time = substr($result[0][0], 44, -7);� $uniques = substr($result2[0][0], 3, -198);␀瘀椀猀椀琀漀爀猀 㴀 猀甀戀猀琀爀⠀␀爀攀猀甀氀琀㈀嬀 崀嬀 崀Ⰰ ㌀㔀Ⰰ ⴀ㘀㘀⤀㬀� $hits = substr($result2[0][0], 136, -64);� $date = “$month | $day | $year | $time”;� $file = fopen(”awstats.txt”, “r”);␀漀氀搀开搀愀琀攀 㴀 昀爀攀愀搀⠀␀昀椀氀攀Ⰰ 昀椀氀攀猀椀稀攀⠀ᴀ愠眀猀琀愀琀猀⸀琀砀琀ᴀ⤠⤀㬀� 椀昀 ⠀␀漀氀搀开搀愀琀攀 ℀㴀 ␀搀愀琀攀⤀ 笀� $file2 = fopen(”awstats.txt”, “r+”);昀眀爀椀琀攀⠀␀昀椀氀攀㈀Ⰰ ␀搀愀琀攀⤀㬀� fclose($file2);␀甀瀀搀愀琀攀搀 㴀 ∀㌀㬠� }� fclose($file);� if ($updated == “1″) {倀爀椀渀琀 ∀� Updated!∀㬀� unset($updated);紀� 倀爀椀渀琀 ∀� Last Update: $month $day, $year - $time㰀琀愀戀氀攀 挀攀氀氀瀀愀搀搀椀渀最㴀尀ᴀ㔠尀ᴀ㸠� 㰀琀搀㸀唀渀椀焀甀攀 嘀椀猀椀琀漀爀猀㨀 㰀⼀琀搀㸀㰀琀搀㸀␀甀渀椀焀甀攀猀㰀⼀琀搀㸀� Total Visitors: $visitors㰀琀搀㸀吀漀琀愀氀 䠀椀琀猀㨀 㰀⼀琀搀㸀㰀琀搀㸀␀栀椀琀猀㰀⼀琀搀㸀� “;� ?>
Hhahahah Sax bro ramro sanga garenau hola ni did you replace the username and password field with yours ?
Ofcourse Chuman.. i did that.. but did not work
I created the script and the .txt file in the same directory, but when I run the PHP for the first time, I get this:
Warning: fread() [function.fread]: Length parameter must be greater than 0 in C:\xampp\htdocs\Misc\quick_stats.php on line 21
Updated! Last Update: , -
Unique Visitors: Total Visitors: Total Hits:
On the second run, I simply get this:
Last Update: , -
Unique Visitors: Total Visitors: Total Hits:
What have I done wrong here?
@dan - you must insert something into the text file (awstats.txt) at least one character (so it’s not 0 bytes in length). That should solve your problem.
@Saxx - I don’t know what you did but you obviously messed up something
The chinese characters have nothing to do with the script, it is something to do with you server (server in china possibly?)
thats a pretty neat trick to use for the sites with awstat. I never even thought that was possible.
great bit of kit but the stats are not very accurate unlike google these stats tell you about visitors even if they have only refreshed the page
[…] http://www.techwhiz.info/2008/03/31/automatically-fetch-data-from-awstats/ […]
Excellent web site I will be visiting often
Hi! Definitely nice and neat site you got there.
hochu vodki!g
It\’s a great and valuable site!t
Very good web site, great work and thank you for your service.
Hello stupid pendosegi.!
I consider that beside Your site there is future!s
I praise God for answering my prayers. God, you are so wonderful, majestic. Jesus Christ, I adore Your Sacred Heart.
Your site is very interesting and useful
A fantastic site, and brilliant effort. A great piece of work.n
Oh o ho! very nice site!s
[…] website knows how important stats are including how many hits your site gets and how much bandwidthhttp://www.techwhiz.info/2008/03/31/automatically-fetch-data-from-awstats/file Include in PHP tutorial and example script with pathphp include script with examples script and […]
Lucky to find you, keep on the good workk guys! Best of luck.l
Very good site! I like it! Thanks!
I praise God for answering my prayers. God, you are so wonderful, majestic. Jesus Christ, I adore Your Sacred Heart.g
Da nu Nahp
Hi, all. Nice site…I really like your site ! Good job man.
Cool guestbook, interesting information… Keep it UP. excellent site i really like your stuff.t
This is very interesting site”
A fantastic site, and brilliant effort. A great piece of work.A
This site is really superb!!! Thank you for you work! Good Luck5
You have built a good websitef
Da nu Nahi
I browse and saw you website and I found it very interesting.Thank you for the good work, greetings
Nice post. I\’ll return.
made it .All information on this site is represented for users. A site is
made it .All information on this site is represented for users. A site isc
Sentimental and nostalgic. Great.u
daite na pivoC
daite na pivo
It\’s a great and valuable site!
Hi! Definitely nice and neat site you got there.
Hello stupid pendosegi.b
Nice post. I\’ll return.e
huyak
I enjoy your site very much! THANK YOUG
Very good web site, great work and thank you for your service.t
Lucky to find you, keep on the good workk guys! Best of luck.
Your site is very interesting and useful
Fascinating site and well worth the visit. I will be back1
Hi, all. Nice site…I really like your site ! Good job man.t
hochu vodki!
This website is Great! I will recommend you to all my friends. I found so much useful things here. Thank you.t
I\’m love this great website. Many thanks guyt
Lucky to find you, keep on the good workk guys! Best of luck.
Looking for information and found it at this great site…
Cool!.. Nice work…