Jump to content

Users Online Problem


kitchen

Recommended Posts

Okay, well I'm working on a users online script, and it tells you how many members are online, and how many guests, only problem is, is if a member logs out - it still says there logged in (until that times out) and then adds them as a guest as well! So, they're basically 2 people.

 

Heres my code, hope someone can help! Thanks in advanced.

 

MySQL

CREATE TABLE `usersonline` (
  `timestamp` int(15) NOT NULL default '0',
  `ip` varchar(255) NOT NULL default '',
  `id` int(11) NOT NULL default '0',
  PRIMARY KEY  (`timestamp`),
  KEY `ip` (`ip`),
  KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

 

PHP

<?

$timestamp = time();
$timeout = $timestamp - 3000;

if(!$info) { $userid = 0; } else { $userid = $info['id']; }

mysql_query("INSERT INTO usersonline VALUES ('$timestamp', '$REMOTE_ADDR', '$userid')");
mysql_query("DELETE FROM useronline WHERE timestamp < $timeout");

$membersresult = mysql_query("SELECT DISTINCT ip FROM usersonline WHERE id > '0'");
$membersonline = mysql_num_rows($membersresult);
$guestsresult = mysql_query("SELECT DISTINCT ip FROM usersonline WHERE id = '0'");
$guestsonline = mysql_num_rows($guestsresult);

if($membersonline == 1) { $u = "user"; } else {	$u = "users"; }
if($guestsonline == 1) { $g = "guest"; } else {	$g = "guests"; }

print "There is $membersonline $u online.<br>";
print "There is $guestsonline $g online.";

?>

 

NOTE: The connect script is in a different file!

Link to comment
https://forums.phpfreaks.com/topic/37731-users-online-problem/
Share on other sites

first, from looking at that you do realize that you are using two different tables in this part:

 

mysql_query("INSERT INTO usersonline VALUES ('$timestamp', '$REMOTE_ADDR', '$userid')");
mysql_query("DELETE FROM useronline WHERE timestamp < $timeout");

 

usersonline and useronline. i am guessing its usersonline, so i'd change the other one if i was you.

 

now, back to the main question.

when they logout just delete them from the list, how hard is that?

on the logout page have a query like this:

 

mysql_query("DELETE FROM usersonline WHERE `ip`='{$_SERVER['REMOTE_ADDR']}'");

 

or before you reset the $info variable, use there id instead of ip. is that easy enough? or is the question different?

Link to comment
https://forums.phpfreaks.com/topic/37731-users-online-problem/#findComment-180553
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.