Jump to content
Sign in to follow this  
freddy

Session Not Properly Logged Out

Recommended Posts

Hi everyone,

 

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.

 

Does anyone know how I can fix this problem?

Share this post


Link to post
Share on other sites

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.

 

Andy

Share this post


Link to post
Share on other sites

I should probably have expanded a little on that answer :(

 

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).

$query ="DELETE FROM $SQL_DB WHERE L_TIME <= time()-300";

I used this method, when I was playing with a script to stop people downloading all my site.

 

If you want to keep the log in the database, then you can simply search for L_TIME :angry:

 

Andy

Share this post


Link to post
Share on other sites

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...

 

It's a temp solution anyway :lol:

 

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?

 

Head Bash

 

I love this Smilie

Share this post


Link to post
Share on other sites

Here's what I did to check who's active... (I've set to time()-60 for testing purposes)

 

>$inactive_time = time()-60;
$inactive_query = "SELECT nick,email FROM users WHERE time >= \"$inactive_time\";";
$do_inactive_query = mysql_query($inactive_query);

echo "Users active: ";
while ($onlineusers = mysql_fetch_array($do_inactive_query)) {
   echo "> <a href=\"mailto:".$onlineusers['email']."\">".$onlineusers['nick']."</a> ";
}

 

I do however HAVE to do the following to make sure the time is updated for active users.

>$ltime = time();
$active_query = "UPDATE users SET time=\"$ltime\" WHERE nick=\"$session_username\";";
$do_active_query = mysql_query($active_query);

 

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. :lol:

 

If you see anything that could be done better here let me know...

thanks for the help! Thumbs Up

Share this post


Link to post
Share on other sites

I've been thinking of chiming in but I have a question:

 

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?

 

Clarifying this will help me/us give you ideas.

Share this post


Link to post
Share on other sites

My goal is to have an active representation of who is online.

 

Being logged in or out is controlled by sessions...

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Found in cpanel.

 

You set up a directive on the server that automatically runs scripts that you choose at time intervals that you choose.

 

You could set up the cron to run a time checking script every five minutes... for example.

 

I wouldn't set it to run more often than one minute... but you could.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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?)

 

two downsides:

1- Too many hits = drain on resources and redundant

2- Not enough hits = infrequent script execution

Share this post


Link to post
Share on other sites

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...

 

I'm just lucky that I can educate my users when and if I implement this system.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...