Page 1 of 1

Identify logged users

Posted: Thu Mar 06, 2003 1:31 pm
by rodrigocaldeira
Hi everyone!

I made a system and this system must to show all actualy logged users. I'm using sessions and MySQL. How can I do this??

Thanks!!

Posted: Thu Mar 06, 2003 2:14 pm
by patrikG
Create a field "loggedIn" (tinyint(1)) in your database (db). If people log in, update the db and set "loggedIn" to 1. Query the database with "SELECT username, loggedIn FROM users WHERE loggedIn='1'".

Posted: Thu Mar 06, 2003 2:18 pm
by rodrigocaldeira
I was thinking to use this, but if people close the browser? How can I know if the user log out?

Posted: Thu Mar 06, 2003 2:27 pm
by patrikG
If I remember correctly PHP-sessions time out after 24mins, it depends on the parameter "session.gc_maxlifetime" in php.ini.

Write the session_id into the db (into "loggedIn"), and when someone refreshes a page, check if the session_id still exists.

Posted: Thu Mar 06, 2003 2:36 pm
by rodrigocaldeira
I'm going to try this right now!

Posted: Thu Mar 06, 2003 2:53 pm
by rodrigocaldeira
How can I know if a session id exists?

Posted: Thu Mar 06, 2003 4:47 pm
by lazy_yogi
Heres the basics of a usersonline script. You'll need to modify it to take care of who ACTUALLY is online. This just says how many usrers are online:

Code: Select all

//===============================================// 
$sql_dbName     = "usersonline";

$db_con = mysql_connect("localhost") 
            OR die ("DB CONNECT Error");

if (! $db_select = mysql_select_db($sql_dbName)) {
    mysql_create_db($sql_dbName)  
           OR die ("DB CREATE Error");
    $db_select = mysql_select_db($sql_dbName)
            OR die("DB CONNECT Error");  
    mysql_query("create table useronline(timestamp int, ip varchar(16))") 
            OR die("Unable to create table");  
}
//===============================================// 

$timeoutseconds   =  200;            //# Timeout value in seconds 
$timestamp = time();  
$timeout = ($timestamp-$timeoutseconds);  
$ip = $_SERVER['REMOTE_ADDR'];

mysql_query("INSERT INTO useronline VALUES('$timestamp', '$ip')")
        OR die("DB INSERT Error");   
mysql_query("DELETE FROM useronline WHERE timestamp<$timeout")   
         OR die("DB DELETE Error");   
$result = mysql_query("SELECT DISTINCT ip FROM useronline")
        OR die("DB SELECT Error");   
$users = mysql_num_rows($result);  

mysql_close();  
if ($users==1)  {  echo "$users User online";    }
else 	    {  echo "$users Users online";   }

Posted: Thu Mar 06, 2003 7:07 pm
by patrikG
Much better with IP, you're right, lazy_yogi :)

Posted: Thu Mar 06, 2003 9:34 pm
by lazy_yogi
well ... you don't necessarily need ip if users login with a username. You can use that.

The main thing is showing an example of the 'timeout' since ppl generally just close the browser and don't log out. This takes care of that as well as possible.

Posted: Fri Mar 07, 2003 6:10 am
by rodrigocaldeira
Thanks Guys!!!

I'll do this and tell you the results!!

Posted: Fri Mar 07, 2003 6:40 am
by rodrigocaldeira
IT'S WORKING !!!!!!!!!!!!!!!!! :D