Page 1 of 1

people on site

Posted: Sun Apr 06, 2003 1:19 am
by Guy
how can i know how many people are currently on my site?
Guy

Posted: Sun Apr 06, 2003 7:59 am
by Kriek

Posted: Mon Apr 07, 2003 8:18 am
by lazy_yogi
Heres what I use :

----------------------- SQL -----------------------

Code: Select all

CREATE TABLE users (
	user_id	INT NOT NULL AUTO_INCREMENT,
	username	VARCHAR(15) NOT NULL,
	password	VARCHAR(32) NOT NULL,

	firstname	VARCHAR(15) NOT NULL,
	surname	VARCHAR(15) NOT NULL,
	phone	VARCHAR(15) NOT NULL,
	email	VARCHAR(15) NOT NULL,

	PRIMARY KEY(user_id)
);
--------------- UsersOnline Script ---------------

Code: Select all

$dbName	= 'dbna,e';
      $username	= 'user';
      $hostname	= 'localhost';
      $password	= 'pass';

      /*** connect to db ***/
      mysql_connect($hostname, $username, $password)
            OR die ("Could not connect: " . mysql_error());
      mysql_select_db($dbName)
            OR die( "Error : ".mysql_errno()." : ".mysql_error()."<br>\n");



      $timeoutseconds   =  300;            //# Timeout value in seconds 
      $timestamp = time();  
      $timeout = ($timestamp-$timeoutseconds);  
      mysql_query("DELETE FROM usersonline WHERE timestamp<$timeout");
      $result = mysql_query( "SELECT * FROM usersonline" )
            or die( "Error : ".mysql_errno()." : ".mysql_error()."<br>\n");


      if (mysql_num_rows($result) == 0 ) {
            print "No users online";
      } else {
            print "<center><table border = 1><tr><th> name </th><th> secs since login </th></tr>";
            while ($row = mysql_fetch_array($result)) {
                  print "<tr><td align = center>".$row['name']."<BR>";
                  print "<td align = center>".(time() - $row['timestamp']);
            }
      }
Then include this page at the top of every page in ur site

Code: Select all

// variables
      $dbName	= 'dbna,e';
      $username	= 'user';
      $hostname	= 'localhost';
      $password	= 'pass';


            /*** connect to db ***/
            mysql_connect($hostname, $username, $password)
                  OR die ("Could not connect: " . mysql_error());
            mysql_select_db($dbName)
                  OR die( "Error : ".mysql_errno()." : ".mysql_error()."<br>\n");



$timeoutseconds   =  300;            //# Timeout value in seconds 
$timestamp = time();  
$timeout = ($timestamp-$timeoutseconds);  

// get user name
$result = mysql_query("select * from users where user_id="".$_SESSION['user_id'].""")
      OR die("Error:".mysql_errno()." : ".mysql_error()."<br>\n");
$row = mysql_fetch_array($result);
$name = $row['firstname']." ".$row['surname'];

mysql_query("DELETE FROM useronline WHERE name='$name'")   or die("DB DELETE Error");   
mysql_query("INSERT INTO useronline VALUES('$timestamp', '$name')")   or die("DB INSERT Error");   
mysql_query("DELETE FROM useronline WHERE timestamp<$timeout")   or die("DB DELETE Error");   


mysql_close();

Posted: Mon Apr 07, 2003 9:08 am
by DeGauss
This can be a tricky issue.

Do you want to track guests AND registered users?

If you want to track guests, you might want to consider using a HEAP type table in mysql.

Create a session var called something like guestSession with a md5 hash of the time + session_id() (to create a more distinct hash) and then insert the generated hash into the guest table. You would need a lastOn column too, which would basically update if the guest has a set session hash. If they do, update that the guest is on.

Then you'd need a bit of code that deletes all guest "accounts" if they're inactive for more than say ten minutes.