Identify logged users

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rodrigocaldeira
Forum Commoner
Posts: 27
Joined: Wed Mar 05, 2003 6:40 pm
Location: Brazil
Contact:

Identify logged users

Post 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!!
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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'".
rodrigocaldeira
Forum Commoner
Posts: 27
Joined: Wed Mar 05, 2003 6:40 pm
Location: Brazil
Contact:

Post by rodrigocaldeira »

I was thinking to use this, but if people close the browser? How can I know if the user log out?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
rodrigocaldeira
Forum Commoner
Posts: 27
Joined: Wed Mar 05, 2003 6:40 pm
Location: Brazil
Contact:

Post by rodrigocaldeira »

I'm going to try this right now!
rodrigocaldeira
Forum Commoner
Posts: 27
Joined: Wed Mar 05, 2003 6:40 pm
Location: Brazil
Contact:

Post by rodrigocaldeira »

How can I know if a session id exists?
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post 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";   }
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Much better with IP, you're right, lazy_yogi :)
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post 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.
rodrigocaldeira
Forum Commoner
Posts: 27
Joined: Wed Mar 05, 2003 6:40 pm
Location: Brazil
Contact:

Post by rodrigocaldeira »

Thanks Guys!!!

I'll do this and tell you the results!!
rodrigocaldeira
Forum Commoner
Posts: 27
Joined: Wed Mar 05, 2003 6:40 pm
Location: Brazil
Contact:

Post by rodrigocaldeira »

IT'S WORKING !!!!!!!!!!!!!!!!! :D
Post Reply