Quote:
Originally posted by Antagony
I'm coding a site that uses mySQL, and I'm trying to figure out a way to determine whether a user is online or offline.
|
i am user "sde" .. you are user "joe" there is no way i can determine whether you are online or not by the cookie on your machine unless the cookie information was writting to a place where i can access information from too .. ( like the database )
here's a general idea of how to database a unix timestamp and then check to see what users are logged in:
PHP Code:
<?
// insert something like this at the top of each page
// the 'lastvisit' field in the db is an int(14)
$now=date();
// this assumes that $user_id is the unique identifier
// and exists in this php script already
$result=mysql_query("update users set lastvisit='$now' where user_id='$user_id'");
?>
that will update the database with a unix timestamp everytime the user goes to a new page ..
so the code to check what users are still logged will be determined by who has a lastvisit later than 5 minutes ago.
PHP Code:
<?
$five_minutes_ago=date()-300;
$result=mysql_query("select user_name from users where lastvisit > '$five_minutes_ago'");
?>
i'm sure there's other methods, but i think this might be easiest.