...

View Full Version : Users Online (needs some modifying)



Blatch
09-04-2009, 07:32 AM
DB: `ip` varchar(255), `timestamp`int(15)


$uip = $_SERVER['REMOTE_ADDR'];
$user_exist = mysql_query("SELECT * FROM `online` WHERE `ip` = '$uip'") or die(mysql_error());

$timeoutseconds = 600; // 10 minutes

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

if(mysql_num_rows($user_exist) > 0) {
mysql_query("UPDATE `online` SET `timestamp` = '$timestamp' WHERE `ip` = '$uip'");
mysql_query("DELETE FROM `online` WHERE `timestamp` < $timeout");
} else {
mysql_query("INSERT INTO `online` (`ip`, `timestamp`) VALUES ('$uip', '$timestamp')");
}
$uonline = mysql_num_rows(mysql_query("SELECT * FROM `online`"));

Can someone fix this for me? I just through this together.. but I want it to update the user's timestamp as they refresh/view another page. I'm doing something wrong :(

(please excuse crappy style. it was rushed. I like to clean things up after)

abduraooft
09-04-2009, 07:40 AM
$uip = $_SERVER['REMOTE_ADDR'];
........
mysql_query("UPDATE `online` SET `timestamp` = '$timestamp' WHERE `ip` = '$ip'"); Anything to do with that?

Blatch
09-04-2009, 07:47 AM
Haha I knew rushing would make me overlook something :P

Thanks!

Now, I wonder, how crappy is my method? >_>
Any suggestions?

Also, should I add the current page the user is viewing? I can't remember what that is PHP_SELF?

abduraooft
09-05-2009, 09:17 AM
$user_exist = mysql_query("SELECT * FROM `online` WHERE `ip` = '$uip'") or die(mysql_error());


I wonder, how crappy is my method? >_>
Any suggestions?
Well, you don't actually need to execute a SELECT before insert. You could set a primary/unique key on the IP filed and then use INSERT .. ON DUPLICATE KEY UPDATE query. Refer http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum