Jump to content

[SOLVED] Who's Online?


bcoffin

Recommended Posts

here's a script I use, that I found on the net.

 

<?php
// Grab all the currently active sessions
function active_sessions() {

/**
*
* TG WHO'S ONLINE
* Copyright 2005 - 2006 (c) TOXIC GOBLIN
* http://www.toxicgoblin.com
* 
*/

//Optional Database Connection Information
//**Uncomment the following 2 lines and edit the values if you do not already have an active database connection**
//

/*
$db = mysql_connect("localhost", "username", "password") or die("Could not connect");
mysql_select_db("database"); */

//Fetch Time
$timestamp = time();
$timeout = $timestamp - 900;

//Insert User
$insert = mysql_query("INSERT INTO cf_whos_online (timestamp, ip, file) VALUES('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')") or die("Error in who's online insert query!");
//Delete Users
$delete = mysql_query("DELETE FROM cf_whos_online WHERE timestamp<$timeout") or die("Error in who's online delete query!");
//Fetch Users Online
$result = mysql_query("SELECT DISTINCT ip FROM cf_whos_online") or die("Error in who's online result query!");
$users = mysql_num_rows($result);

//Show Who's Online
if($users == 1) {
print("Online Now: $users");
} else {
print("Online Now: $users");
}

};

?>

 

Regards ACE

Link to comment
Share on other sites

The table isn't that big, so these queries shouldn't that expensive, so a little overhead but not that big a deal.  Should put an index on the timestamp column.

 

But this is strange -- it counts users more than once if they change pages rapidly. Plus DISTINCT's may be expensive. Should use count(distinct ip) at least. Or just cut out the "file" column and use count(*) which is a million times faster.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.