RSS Feed

Online User Counter simple script

This script gives an accurate count of the number of users online. Very easy to add to webpages, just insert the line "" and it would display the string "xx users online" (where xx is the number of users). Instead of depending on 3rd party hosted solutions create your own targeted online users script. This is a modified Mitch script (see references below), with a few enhancements added to handle proxies and collisions.

Corrections: mysql_connect, passing the correct db param





// Configuration
$dbhost = "localhost";
$dbuser = "[to-add]"; // MySQL Username
$dbpass = "[to-add]"; // MySQL Password
$dbname = "[to-add]"; // Database Name
$timeoutseconds = 1200; // length of session, 20 minutes is the standard

$timestamp=time();
$timeout=$timestamp-$timeoutseconds;
$ip = substr($REMOTE_ADDR, 0, strrpos($REMOTE_ADDR,"."));

// Connect to MySQL Database
@mysql_connect($dbhost,$dbuser,$dbpass);
@mysql_select_db($dbname) or die("No db");

// Add this user to database
$loopcap = 0;
while ($loopcap<3 && @mysql_query("insert into useronline values('$timestamp','$ip','$PHP_SELF')"))
{ // in case of collision
$timestamp = $timestamp+$ip{0}; $loopcap++;
}

// Delete users that have been online for more then "$timeoutseconds" seconds
@mysql_query("delete from useronline where timestamp<$timeout");

// Select users online
$result = @mysql_query("select distinct ip from useronline");
$user = @mysql_num_rows($result);

mysql_free_result($result);
@mysql_close();

// Show all users online
if ($user==1) {echo $user.' user online';} else {echo $user.' users online';}
?>


Step by step overview of the script:

  1. Retrieve the first 3 tuples of the IP (nnn.nnn.nnn)
  2. Insert the row
  3. Delete old rows
  4. Get count of online users

1. Why the first 3 tuples of an IP?

Because of proxies, like the AOL proxy, AOL users daisy chain through a set of IP's that belong to a proxy. Each HTTP request would use an IP from a pool of IP's. Normally an IP address remains static per session, now with proxies, every single http request could be on a different IP, however these IP's mostly belong to a specific subnet, thus we retrieve the first 3 tuples of the IP.

2. Why the loop cap and timestamp addition?

Since the key of the table is the timestamp, the timestamp needs to be unique. Sometimes a collision occurs, one user gets the timestamp, the other and error stating that the key is already taken. To manage this we (a) suppress the error checking, by adding an '@' symbol in front of the PHP function, (2) increment the timestamp by 1 or 2 and try to insert again and (c) cap the tries to 3 in case the site is getting a HUGE amount of traffic (actually what we need to do is to track the load of the server, if the load is too high then we should start dropping requests).


3. Delete rows

The script will check each row to see if it exceeds the alloted time. If so, that row is deleted.


4. Display counter, online users

Select the number of unique IP's in the table. Get a count and display the results, that will give you the number of online users.



Create SQL table

Be sure to create your table first before running the script
CREATE TABLE `useronline` ( `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`) ) TYPE=MyISAM;
Why is the timeout/session 20 minutes long?

Wanted the "online users" to be comparable to other sites, the default timeouts seem to be set at 20 minutes. This number is changeable if you wish to compare to sites that have different session lengths.


Or

conf.php


// Configuration
$dbhost = "localhost";
$dbuser = "root"; // MySQL Username
$dbpass = ""; // MySQL Password
$dbname = "nono"; // Database Name
$timeoutseconds = 1200; // length of session, 20 minutes is the standard

$timestamp=time();
$timeout=$timestamp-$timeoutseconds;
$ip = substr($REMOTE_ADDR, 0, strrpos($REMOTE_ADDR,"."));

// Connect to MySQL Database
@mysql_connect($dbhost,$dbuser,$dbpass);
@mysql_select_db($dbname) or die("No db");
?>



useronline.php



include('conf.php');

// Add this user to database
$loopcap = 0;
while ($loopcap<3 && @mysql_query("insert into useronline values('$timestamp','$ip','$PHP_SELF')"))
{ // in case of collision
$timestamp = $timestamp+$ip{0}; $loopcap++;
}

// Delete users that have been online for more then "$timeoutseconds" seconds
@mysql_query("delete from useronline where timestamp<$timeout");

// Select users online
$result = @mysql_query("select distinct ip from useronline");
$user = @mysql_num_rows($result);

mysql_free_result($result);
@mysql_close();

// Show all users online
if ($user==1) {echo $user.' user online';} else {echo $user.' users online';}
?>
Stumble
Delicious
Technorati
Twitter
Facebook

0 commentaires:

Post a Comment

 
****

****