Session Not Properly Logged Out

liunx

Guest
Hi everyone,<br /><br />i'm kinda working a user account system using php sessions and mysql. Sofar I have been able to create the everything except for one thing. Lets say a user logs in, I then set the user_active record to 1 meaning the user is logged in. If the user logs out by clicking the logout button the record is set to 0 meaning he has logged out. This al works fine, but! when a user logs in and later on closes his browser without clicking log out the session is destroyed of course and the user is logged out but user_active record remains on 1.<br /><br />Does anyone know how I can fix this problem?<!--content-->
I would check the time they have been inactive and log them out (set the record to 0) if they havn't been active in say 5 mins.<br /><br />Andy<!--content-->
I should probably have expanded a little on that answer <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/rolleyes.gif" style="vertical-align:middle" emoid=":rolleyes:" border="0" alt="rolleyes.gif" /> <br /><br />If you don't want to keep the records, then whenever you run the script for tracking users you can simply delete records older than a given time. If you have a variable L_TIME which you always set to the latest time a user did something, then you can get the time now and check for any records with L_TIME less than time() - 300 (for 5 mins).<br /><!--quoteo--><div class='quotetop'>QUOTE</div><div class='quotemain'><!--quotec-->$query ="DELETE FROM $SQL_DB WHERE L_TIME <= time()-300";<!--QuoteEnd--></div><!--QuoteEEnd--><br />I used this method, when I was playing with a script to stop people downloading all my site.<br /><br />If you want to keep the log in the database, then you can simply search for L_TIME <= time()-300 and user_active = 1, setting user_active = 0 for any cases you find.<br /><br />It may be a bit simplistic for your needs, but I like simplistic <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/biggrin.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" /> <br /><br />Andy<!--content-->
I guess I will have to go with that then. It's not really accurate since after loggin in you might be busy more then 5 minutes on one page and another user will set your status to "0" and you will not show up as being online untill you go to another page. I know, you can set it to 15 or even 30 minutes but still...<br /><br />It's a temp solution anyway <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><br /><br />Still wondering if there is a better way to do it... Maybe by giving the "idle" user somekind of notice that he is about to timeout or something but to do that in php?<br /><br /> Head Bash <br /><br />I love this Smilie<!--content-->
Here's what I did to check who's active... (I've set to time()-60 for testing purposes)<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$inactive_time = time()-60;<br />$inactive_query = "SELECT nick,email FROM users WHERE time >= \"$inactive_time\";";<br />$do_inactive_query = mysql_query($inactive_query);<br /><br />echo "Users active: ";<br />while ($onlineusers = mysql_fetch_array($do_inactive_query)) {<br />    echo "> <a href=http://www.totalchoicehosting.com/forums/lofiversion/index.php/\"mailto:".$onlineusers['email']."\">".$onlineusers['nick']."</a>&nbsp;";<br />}<!--c2--></div><!--ec2--><br /><br />I do however HAVE to do the following to make sure the time is updated for active users.<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$ltime = time();<br />$active_query = "UPDATE users SET time=\"$ltime\" WHERE nick=\"$session_username\";";<br />$do_active_query = mysql_query($active_query);<!--c2--></div><!--ec2--><br /><br />It's not quite like what you offered but this works just fine... I'm not that great with php and this is basicly just a learning experience for me, it's my first user login and management system I'm making. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><br /><br />If you see anything that could be done better here let me know...<br />thanks for the help! Thumbs Up<!--content-->
I've been thinking of chiming in but I have a question:<br /><br />What is your purpose? Do you want users to be logged out after a certain time? Or is your goal to have an active representation of who is online?<br /><br />Clarifying this will help me/us give you ideas.<!--content-->
My goal is to have an active representation of who is online.<br /><br />Being logged in or out is controlled by sessions...<!--content-->
Okay. Then I would go with the ideas that were presented by Andy B and activate with a cron or some other script if you get enough traffic. Cron would probably be best.<!--content-->
What is a cron? <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
Found in cpanel. <br /><br />You set up a directive on the server that automatically runs scripts that you choose at time intervals that you choose.<br /><br />You could set up the cron to run a time checking script every five minutes... for example.<br /><br />I wouldn't set it to run more often than one minute... but you could.<!--content-->
Ok, ic ... well I don't think that's needed in this case. I suppose a window of 10 to 15 minutes is good enough. How do you think this forum checks it?<!--content-->
The only other way to trigger regular execution of a script is to have it called (included?) in a page of your site that gets hit often (index?)<br /><br />two downsides:<br />1- Too many hits = drain on resources and redundant<br />2- Not enough hits = infrequent script execution<!--content-->
I was initially trying to find a way to catch a user that closes his browser without actually logging out. The only way to do that is to have a pop-up windows that auto closes when the script is done but then again you can block popups...<br /><br />I'm just lucky that I can educate my users when and if I implement this system.<!--content-->
 
Back
Top