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!

65 Responses to “Automatically Fetch Data From AWStats”

  1. Good tutorial man.Love it

    April 3rd, 2008 | 4:43 am
  2. Great tutorial

    April 4th, 2008 | 11:45 pm
  3. Getting an error:
    Parse error: syntax error, unexpected ‘:’ in C:\wamp\www\stats.php on line 3
    =\

    Great tutorial though, Please help :D

    April 9th, 2008 | 9:29 am
  4. @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!

    April 9th, 2008 | 9:21 pm
  5. pleh

    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

    April 18th, 2008 | 1:49 pm
  6. @pleh

    He isnt stealing passwords thats simply an example you replace techwhiz.info with your domain

    April 18th, 2008 | 5:47 pm
  7. 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…

    April 21st, 2008 | 8:51 am
  8. 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/

    April 21st, 2008 | 8:52 am
  9. Sweet script dude. Congrats and keep up the good work.

    April 22nd, 2008 | 5:07 am
  10. Barry

    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

    April 24th, 2008 | 7:03 am
  11. SAXX

    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਀㰀琀搀㸀吀漀琀愀氀 䠀椀琀猀㨀 㰀⼀琀搀㸀㰀琀搀㸀␀栀椀琀猀㰀⼀琀搀㸀� “;਀� ?>

    May 2nd, 2008 | 12:32 pm
  12. Hhahahah Sax bro ramro sanga garenau hola ni did you replace the username and password field with yours ?

    May 2nd, 2008 | 6:54 pm
  13. SAXX

    Ofcourse Chuman.. i did that.. but did not work

    May 3rd, 2008 | 11:50 am
  14. Dan

    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?

    May 3rd, 2008 | 4:49 pm
  15. @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?)

    May 4th, 2008 | 8:18 pm
  16. thats a pretty neat trick to use for the sites with awstat. I never even thought that was possible.

    May 19th, 2008 | 5:54 am
  17. 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

    May 21st, 2008 | 8:18 am
  18. May 29th, 2008 | 9:53 am
  19. Excellent web site I will be visiting often

    June 1st, 2008 | 5:25 pm
  20. Ron

    Hi! Definitely nice and neat site you got there.

    June 1st, 2008 | 9:59 pm
  21. hochu vodki!g

    June 2nd, 2008 | 2:04 am
  22. It\’s a great and valuable site!t

    June 2nd, 2008 | 2:07 am
  23. Very good web site, great work and thank you for your service.

    June 2nd, 2008 | 4:02 am
  24. Hello stupid pendosegi.!

    June 2nd, 2008 | 9:43 pm
  25. I consider that beside Your site there is future!s

    June 3rd, 2008 | 12:35 am
  26. I praise God for answering my prayers. God, you are so wonderful, majestic. Jesus Christ, I adore Your Sacred Heart.

    June 3rd, 2008 | 5:15 am
  27. Your site is very interesting and useful

    June 3rd, 2008 | 12:55 pm
  28. A fantastic site, and brilliant effort. A great piece of work.n

    June 3rd, 2008 | 1:33 pm
  29. Oh o ho! very nice site!s

    June 3rd, 2008 | 2:19 pm
  30. […] 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 […]

    June 3rd, 2008 | 5:14 pm
  31. Lucky to find you, keep on the good workk guys! Best of luck.l

    June 4th, 2008 | 5:23 am
  32. Very good site! I like it! Thanks!

    June 4th, 2008 | 11:15 am
  33. I praise God for answering my prayers. God, you are so wonderful, majestic. Jesus Christ, I adore Your Sacred Heart.g

    June 4th, 2008 | 3:48 pm
  34. Da nu Nahp

    June 5th, 2008 | 1:43 pm
  35. Hi, all. Nice site…I really like your site ! Good job man.

    June 5th, 2008 | 2:58 pm
  36. Cool guestbook, interesting information… Keep it UP. excellent site i really like your stuff.t

    June 5th, 2008 | 3:59 pm
  37. This is very interesting site”

    June 5th, 2008 | 6:02 pm
  38. A fantastic site, and brilliant effort. A great piece of work.A

    June 5th, 2008 | 7:11 pm
  39. This site is really superb!!! Thank you for you work! Good Luck5

    June 19th, 2008 | 3:30 pm
  40. You have built a good websitef

    June 19th, 2008 | 4:49 pm
  41. Dan

    Da nu Nahi

    June 20th, 2008 | 12:20 am
  42. I browse and saw you website and I found it very interesting.Thank you for the good work, greetings

    June 20th, 2008 | 2:24 am
  43. Nice post. I\’ll return.

    June 21st, 2008 | 6:45 am
  44. made it .All information on this site is represented for users. A site is

    June 21st, 2008 | 6:00 pm
  45. made it .All information on this site is represented for users. A site isc

    June 22nd, 2008 | 7:57 am
  46. Sentimental and nostalgic. Great.u

    June 22nd, 2008 | 3:33 pm
  47. daite na pivoC

    June 22nd, 2008 | 7:37 pm
  48. daite na pivo

    June 24th, 2008 | 6:20 am
  49. It\’s a great and valuable site!

    June 24th, 2008 | 7:17 am
  50. Hi! Definitely nice and neat site you got there.

    June 24th, 2008 | 2:02 pm
  51. Hello stupid pendosegi.b

    June 25th, 2008 | 3:08 pm
  52. Ron

    Nice post. I\’ll return.e

    June 27th, 2008 | 11:05 am
  53. huyak

    June 28th, 2008 | 5:49 pm
  54. I enjoy your site very much! THANK YOUG

    June 28th, 2008 | 5:49 pm
  55. Very good web site, great work and thank you for your service.t

    June 29th, 2008 | 4:08 am
  56. Lucky to find you, keep on the good workk guys! Best of luck.

    June 29th, 2008 | 12:50 pm
  57. Your site is very interesting and useful

    June 30th, 2008 | 10:06 pm
  58. Fascinating site and well worth the visit. I will be back1

    July 1st, 2008 | 1:54 pm
  59. Hi, all. Nice site…I really like your site ! Good job man.t

    July 1st, 2008 | 2:26 pm
  60. hochu vodki!

    July 1st, 2008 | 4:06 pm
  61. This website is Great! I will recommend you to all my friends. I found so much useful things here. Thank you.t

    July 1st, 2008 | 4:08 pm
  62. I\’m love this great website. Many thanks guyt

    July 1st, 2008 | 11:15 pm
  63. Lucky to find you, keep on the good workk guys! Best of luck.

    July 1st, 2008 | 11:53 pm
  64. Looking for information and found it at this great site…

    July 2nd, 2008 | 7:27 am
  65. Cool!.. Nice work…

    July 4th, 2008 | 10:28 am

Leave a Reply