[RESOLVED] *Sigh* Printing logged in users (Sessions)
Hi,
I have a login code that uses Sessions to store the users data. However, I would like to display the logged in users to the rest of the users, ie. on index.php.
Example:
Logged in users: Rudi, test, adminstrator
How would I be able to create a record of who's logged in, and then automatically delete it when the user's session expires, in order to make this process almost "real-time".
I hope you understand what I mean.
Rudi :wave:
Re: *Sigh* Printing logged in users (Sessions)
You could create a table which contains the list of users who are current active on your site, and when their "session" expires. So each time a user views a page it updates their record in the table and sets their "session" to expire in a couple of minutes time. The you can just read all the records from the table, removing any records where the "session" has expired.
Note: I am not refering to the actual $_SESSION expiring but if the user has become inactive.
Re: *Sigh* Printing logged in users (Sessions)
Quote:
Originally Posted by john tindell
You could create a table which contains the list of users who are current active on your site, and when their "session" expires. So each time a user views a page it updates their record in the table and sets their "session" to expire in a couple of minutes time. The you can just read all the records from the table, removing any records where the "session" has expired.
Note: I am not refering to the actual $_SESSION expiring but if the user has become inactive.
Do you have any possible code, or a MySQL Table Structure that I could use.
I have:
CREATE TABLE loggedusers (
username VARCHAR(30) NOT NULL,
timestamp TIMESTAMP DEFAULT NOW(),
PRIMARY KEY (userid)) TYPE=InnoDB ;
So far that will store the entries each time they visit a page (Maybe header.php (Loaded on EVERY page) could contain some time updating code, but what would that code be??)
Also, how to remove the records after a certain time, in an automatic process??
:sick:
Re: *Sigh* Printing logged in users (Sessions)
I use the session id as the PK.
This is how i clean up the in-active users if they haven't done anything after 5 minutes
Code:
DELETE FROM your_table WHERE session_id = '". session_id() . "' OR DATE_SUB(now(), interval 5 minute) >= timestamp
All you need to do is when a user requests a page just insert into the table their session id and use the now() function from mysql to insert the current time. This will clean up the table and update the records so you have an uptodate account of whats happening. You can also extend the table to contain information about what page they are viewing, what browser they are using as well.
Re: [RESOLVED] *Sigh* Printing logged in users (Sessions)
Re: [RESOLVED] *Sigh* Printing logged in users (Sessions)
I did sumtin similar a while back.... My only problem with my way was...It was Screwing wit the the sessions...By deleting them.
I'll give this a go for that project...Thanks.
Re: [RESOLVED] *Sigh* Printing logged in users (Sessions)
Well what I did exactly was the following.
Create a table with the username and time (Maybe UserID for you..):
CREATE TABLE loggedusers (
username VARCHAR(30) NOT NULL,
timer BIGINT(30)
) TYPE=InnoDB ;
Then very simply, in the header.php (Which is included on EVERY page):
PHP Code:
if (isset($_SESSION['user'])) {
$valquery = mysql_query("SELECT * FROM loggedusers WHERE username = '".$_SESSION['user']."' ;");
$valresult = mysql_num_rows($valquery);
if ($valresult == "1") {
mysql_query("UPDATE loggedusers SET `timer` = UNIX_TIMESTAMP() WHERE username = '".$_SESSION['user']."'");
} else {
mysql_query(" INSERT INTO `loggedusers` ( `username` , `timer`) VALUES ('".$_SESSION['user']."', UNIX_TIMESTAMP()) ;");
}
}
mysql_query("DELETE FROM loggedusers WHERE timer<(UNIX_TIMESTAMP()-(120));");
And that works perfectly well for each 2 minutes of clearing the database... :)
It's all rather self-explanitory, just change the $_SESSION['user'] to whatever variable in the session holds the username. And then the UNIX_TIMESTAMP()-(120)) part, change the 120 to the amount of seconds you want to remove old people from the database.
Also, this way, if the user returns to the page, it will automatically re-insert them into the database if they are not there already without having to re-login.