Drupal Development Blog is brought to you by Face 2 Interface, home of open source applications for your website.
Drupal Development Blog is brought to you by Face 2 Interface, home of open source applications for your website.
I wanted to add a hit counter to my log report.
It's a pretty basic thing, in fact adding a log and hit counter to a website was the very first thing I learned years ago when getting into Perl and CGI programming, and it's still good to see. This site is new, I've barely started getting a bit of search engine coverage and can see the occasional anonymous user plus one acquaintance who's registered.. thanks Josh.
Here's what I did.
First I went to the Administer > Logs > Recent hits screen. Then found where that screen is generated; not hard to do, it's in the modules/statistics/statistics.module and the function is called "statistics_recent_hits".
This is what it looked like out of the box:
/**
* Menu callback; presents the "recent hits" page.
*/
function statistics_recent_hits() {
$header = array(
array('data' => t('Timestamp'), 'field' => 'a.timestamp', 'sort' => 'desc'),
array('data' => t('Page'), 'field' => 'a.path'),
array('data' => t('User'), 'field' => 'u.name'),
array('data' => t('Operations'))
);
$sql = 'SELECT a.aid, a.path, a.title, a.uid, u.name, a.timestamp FROM {accesslog} a LEFT JOIN {users} u ON u.uid = a.uid' . tablesort_sql($header);
$result = pager_query($sql, 30);
while ($log = db_fetch_object($result)) {
$rows[] = array(
array('data' => format_date($log->timestamp, 'small'), 'class' => 'nowrap'),
_statistics_format_item($log->title, $log->path),
theme('username', $log),
l(t('details'), "admin/logs/access/$log->aid"));
}
$output = theme('table', $header, $rows);
$output .= theme('pager', NULL, 30, 0);
return $output;
}
The DB query goes against the accesslog table, and going to phpMyAdmin and then looking at the table description for the accesslog could see that the first field, aid, is defined as auto_increment. So that means it is a counter, and a phpMyAdmin query on that field quickly showed that.
So in order to add the hit num data to the 'recent hits' display I only have to add the column to the header display, and the field value to the data display. For good measure I also changed the sorting sequence from timestamp to hit num (aid). Here is the revised function with my changes commented:
/**
* Menu callback; presents the "recent hits" page.
*/
function statistics_recent_hits() {
$header = array(
array('data' => t('Hit'), 'field' => 'a.aid', 'sort' => 'desc'), // ml 2007-06-14
array('data' => t('Timestamp'), 'field' => 'a.timestamp'), // ml 2007-06-14
array('data' => t('Page'), 'field' => 'a.path'),
array('data' => t('User'), 'field' => 'u.name'),
array('data' => t('Operations'))
);
$sql = 'SELECT a.aid, a.path, a.title, a.uid, u.name, a.timestamp FROM {accesslog} a LEFT JOIN {users} u ON u.uid = a.uid' . tablesort_sql($header);
$result = pager_query($sql, 30);
while ($log = db_fetch_object($result)) {
$rows[] = array(
$log->aid, // ml 2007-06-14
array('data' => format_date($log->timestamp, 'small'), 'class' => 'nowrap'),
_statistics_format_item($log->title, $log->path),
theme('username', $log),
l(t('details'), "admin/logs/access/$log->aid"));
}
$output = theme('table', $header, $rows);
$output .= theme('pager', NULL, 30, 0);
return $output;
}
Needless to say I first renamed the my existing (and working) statistics.module on the server to statistics.module_bkp.