|
-
Jun 14th, 2006, 04:21 PM
#1
Thread Starter
Hyperactive Member
[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
-
Jun 14th, 2006, 04:26 PM
#2
<?="Moderator"?>
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.
-
Jun 14th, 2006, 04:33 PM
#3
Thread Starter
Hyperactive Member
Re: *Sigh* Printing logged in users (Sessions)
 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??
-
Jun 14th, 2006, 04:48 PM
#4
<?="Moderator"?>
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.
-
Jun 14th, 2006, 05:06 PM
#5
Thread Starter
Hyperactive Member
Re: [RESOLVED] *Sigh* Printing logged in users (Sessions)
-
Jun 14th, 2006, 05:54 PM
#6
Hyperactive Member
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.
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Jun 15th, 2006, 03:38 AM
#7
Thread Starter
Hyperactive Member
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.
Last edited by ..:RUDI:..; Jun 15th, 2006 at 03:43 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|