Modern widget engines (Apple Dashboard, Yahoo!Widgets, Windows Vista Sidebar) use XML transport (AJAX) to constantly update various information(like weather, stocks, etc) from web. In this article there is a source of trivial php web-counter which collects stats and outputs number of page views(hits) and visitors(in fact, unique hosts) since midnight as XML file. There is also our special AeCounter Dashboard Widget (and it's sources) which receive these xml-statistics (with the help of XmlHttpRequest object) and displays numbers on a little nice panel.
First you need in set up server part: our xml-based php web-counter.
The counter uses two MySQL tables: ae_counter_day ( stores number of hits and current day ), ae_counter_hosts (stores all hosts for a current day). These tables are quite trivial. In fact, we could use only one table(ae_counter_hosts), but since we wanted our counter to remove old unnecessary data (for previous day) we created table which stores current day value.
Execute this SQL queries to create statistics tables:
source code: SQL / MySQL
CREATE TABLE `ae_counter_day` ( `day` date NOT NULL DEFAULT '0000-00-00', `hits` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`day`) ) ENGINE=MyISAM; CREATE TABLE `ae_counter_hosts` ( `day` date NOT NULL DEFAULT '0000-00-00', `ip` char(16) NOT NULL DEFAULT '', KEY `ip` (`ip`), KEY `day` (`day`) ) ENGINE=MyISAM;
PHP web-counter has two modes of operation: counter (add +1 hit to hits to ae_counter_day row and IP address to ae_counter_hosts table) and 'xml results output' which provides output to XmlHttpRequest-based application (widget).
source code: PHP
Don't forget to create sql-tables (execute CREATE TABLE queries above) and change variables ($db_host, $db_user, $db_pwd, $database) to your MySQL / database settings.
There are two techniques of embedding this counter to your web-pages:
source code: HTML
<!-- change /path/to/saved/counter.php to real path of counter script --> <img src="/path/to/saved/counter.php?gif=1" width="1" height="1" border="0" alt="" />
To check your web-counter installation, visit web-page where you've embedded web-counter. Then navigate to http://your-web-site-url/path/counter.php?show=1 (place at your web site where you've saved counter script) and check it's output. The script should output XML file like:
source code: XML
<anyexample-counter> <hits>6</hits> <hosts>3</hosts> </anyexample-counter>
Try visiting your site again and checking counter statistics afterwards.
If everything is fine, you may download our Apple Dashboard Widget, insert your counter url at it's preferences(widget back) and see your web-page statistics at dashboard. 'AeCounter' widget will update statistics every 15 minutes.
Mac OS X 10.4 Tiger is required. If you're using Safari, click the download link. When the widget download is complete, show Dashboard, click the Plus sign to display the Widget Bar and click the widget's icon in the Widget Bar to open it. If you're using a browser other than Safari, click the download link. When the widget download is complete, unarchive it and place it in /Library/Widgets/
Additionally, we provide sources of this widget as a Dashcode project.
You may also download PHP and MySQL sources for this article.
Feedbacks on this article are gladly accepted.