custom simple counter

dragon2000 - March 27, 2009 - 12:39

Hi,
I want to create a simple counter that just can record the number of visitors.
should I use variable_get() and variable_set() or create a table to record this.
what is your suggestion?
thanks.

Is this piece of code

dragon2000 - March 27, 2009 - 16:04

Is this piece of code optimized to do this?

$ct = variable_get('mcounter_ct',1);
variable_set('mcounter_ct',++$ct);
$output .='Site counter:'.$ct;

thanks in advance...

core statistics

grendzy - March 28, 2009 - 01:17

The core statistics module will do this for you.

Your code isn't going to work well, because it contains a race condition. (Imagine if 10 users load the page all at the same time, the counter will only get incremented once).

thanks... I want add a

dragon2000 - March 28, 2009 - 09:41

thanks...
I want add a counter to record site visits , but statistics module counts the number of each post visits .

How can I solve the race condition problem of my code?

thanks again...

To resolve the race

grendzy - March 28, 2009 - 20:11

To resolve the race condition, you can increment a database field directly in SQL:

<?php
db_query
('UPDATE {mcounter} SET count = count + 1');
?>

You might also consider browsing the statistics module category. I found a few that might help you:

Your hosting provider also probably offers statistics via awstats or webalizer. And of course there's the heavyweight, Google Analytics.

If it sounds like I'm trying to talk you out of writing your own stats module... it's because getting accurate counts is pretty hard. All sorts of factors can skew the numbers, like robots and spiders, ISP proxies, internet worms, users that reject your cookies, and the list goes on.

Good luck!

 
 

Drupal is a registered trademark of Dries Buytaert.