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
https://forums.phpfreaks.com/topic/78598-solved-whos-online/#findComment-397820
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
https://forums.phpfreaks.com/topic/78598-solved-whos-online/#findComment-397829
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.