Hit Counter & Users Online Script

Hey everyone, this is just a simple script which will tell you how many people are online on your site and how many pages hits you’ve had in both the last 24 hours and all up.

Create database and place this dump inside it:

# Table structure for table `counter`

CREATE TABLE `counter` (
`counter` text NOT NULL

# Dumping data for table `counter`

INSERT INTO `counter` (`counter`) VALUES ('0');
# --------------------------------------------------------

# Table structure for table `daily_hits`

CREATE TABLE `daily_hits` (
`date` text NOT NULL,
`ip` text NOT NULL

# Dumping data for table `daily_hits`

# Table structure for table `usersonline`

CREATE TABLE `usersonline` (
`timestamp` int(15) NOT NULL default '0',
`ip` varchar(40) NOT NULL default '',
`file` varchar(100) NOT NULL default '',
PRIMARY KEY (`timestamp`),
KEY `ip` (`ip`),
KEY `file` (`file`)

# Dumping data for table `usersonline`

INSERT INTO `usersonline` (`timestamp`, `ip`, `file`) VALUES (1035423939, '', 'Mailing List');

Next you have to make the background script which will run this baby!

Create a file called: stats.php , and place this inside it.

@mysql_connect( "localhost","username","password" );
@mysql_select_db( "database_name" );
$date = date( "j/m/Y" );
$daily1 = "INSERT INTO daily_hits 
           (date, ip) 
           ('$date', '$REMOTE_ADDR') ";
$re = mysql_query( $daily1 );
$delete = "DELETE FROM daily_hits 
           WHERE date <> '$date' ";

$del = mysql_query( $delete );
$main_counter = mysql_query( "SELECT counter 
                              FROM counter " );
$gy = mysql_fetch_array( $main_counter );
$new_count = $gy['counter'] + 1;
$up = mysql_query( "UPDATE counter 
                    SET counter='$new_count'" );
$sql = mysql_query( "SELECT * 
                     FROM users" );
$users = mysql_num_rows( $sql );

$sql = mysql_query( "SELECT * 
                     FROM daily_hits" );
$daily = mysql_num_rows( $sql );

$sql = mysql_query("select counter from counter");
$count = mysql_fetch_array($sql);

$timeoutseconds = 30; 
$timestamp = time(); 
$timeout = $timestamp-$timeoutseconds; 
$insert = mysql_query("INSERT INTO usersonline VALUES ( '$timestamp','$REMOTE_ADDR','$page' ) " );
if ( !( $insert ) ) 
print "Useronline Insert Failed > "; 
$delete = mysql_query( "DELETE FROM usersonline WHERE timestamp<$timeout" ); 

if (!( $delete ) ) 
print "Useronline Delete Failed > ";
$result = mysql_query( "SELECT DISTINCT ip FROM usersonline" ); 
if ( !( $result ) ) 
print "Useronline Select Error > "; 
$user = mysql_num_rows( $result ); 
if ( $user == '1' ) 
$online = "$user";
} else { 
$online = "$user"; 


Now thats the hard work done, all you have to do now is call that script onto your page:

On the page you want the counter, place this before anything else on the page (Before tag ):

include( "/path/to/stats.php" );

Now, where you want the users online and site hits to be placed on your page just put this code:

<b><?=$online ?></b> user online. With: <b><?=$daily ?></b> Pages views today, and a total of: <b><?php echo $count[counter] ?></b> views.

This will be the first in a number of tutorials i’ll be putting up. Hope this helps someone.

Hmm, interesting, I may give it a try.


unfortunately that script doesn’t work as an addon to a membership as is. You would have to make variations to it for that sort of system to work.


thanks for the correction. I have fixed it and it should be all good now.

n00b here… how do you make a database? ahh im hopin this isnt too complicated/confusing…

hey, the code works GREAT cept 1 thing, it says this…

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in c:appservwwwstats.php on line 26
heres line 26

$users = mysql_num_rows( $sql );

any suggestions, thank you

Can such a script accurately count 500 to 1000 hits per minute? What is maximum load this has been tested with?

thx searcher

A website with a hit counter will never recieve 500-1000 hits a minute. Imaging Yahoo with a hit counter… That would be horrible. Hit counters are about the best way to get a site to look amateur. You can count your visitors for internal use, but no need to display it, no one care. When you reach a website, you don’t want to know how many people saw it, you want to see the actual content.

MySQL probably won’t scale to 500-1000 hits a minute, not without replication. I don’t know if you calculated, but it’s 720k to 1.4m hits a day.

I get 120,000 to 180,000 hits a day, depending on day of the week. At peak load during mid-day I’m getting 500 to 800 hits per minute (and growing). I don’t get that all day, but I do get it for several hours straight, which is what the problem is with counters. You may feel a visible counter is a sign of a “amateur”, but when it is used for marketing purposes, a little counter can make or break a sale. Maybe individual viewers could care less about hits, views and clickthroughs, but paying advertisers do. If a visible page counter gives my customers a little reassurance, then I’ll use one. Question is which will do the job with less server load, new PHP/MySQL sollution or current Perl cgi?

Apache automatically generates a log file with the request. Simply get a cron task to run every minute to count how many hits you had. When you need massive performences, avoid using databases. Cache as much files statically as you can. Database connections and requests are always the slowest part in a script, even if they are very useful and required. Some pages simply don’t need to be all that dynamic.

An other solution is to use SRM, which is a deamon to create PHP application variables. Using a counter with that would be a lot faster. It’s not yet the most stable application around, but if you are serious, you might want to work on it, I think Derick could use some help.

unfortunately that script doesn’t work as an addon to a membership as is. You would have to make variations to it for that sort of system to work

Sponsor our Newsletter | Privacy Policy | Terms of Service