Jump to content

Recommended Posts

Hallo php World,

 

I am tracking with a variable in db called 'Logged' and setting it to 1 if a user logged in and setting session at this point as well. If the user logout then it is set to 0. This way I am allowing only one login per user name & Password.

 

But I have a problem that If the user didn't log out, them the 'Logged' flag is not changed. thus preventing the user logging in again.

 

Question : How can I know if the user just closed the browser, Or inactive for more than 20min.

 

thank you for any help!

Link to comment
https://forums.phpfreaks.com/topic/55380-session-time-out/
Share on other sites

Try creating a special column named something like, `user_time`. At the top of every page you can run a statement such as:

 

mysql_query('UPDATE `users` SET `user_time` = UNIX_TIMESTAMP() WHERE `user_id` = "INSERT AN ID"');

 

This will in turn update their time every time they load a page. You can then use something like:

 

mysql_query('SELECT `user_time` FROM `users` WHERE `user_id` = "INSERT AN ID"');

;

 

That will select the users time from the db for whatever user you choose, then if you take that timestamp and compare it to the current time you can see if they have been online in the past 10 minutes.

 

Something like this will do

if ($user_time > time() - 900) {
    echo 'The user has loaded a page in the last 15 minutes.';
} else {
    echo 'The user has not loaded a page in the last 15 minutes, they probably closed the window.';
}

Link to comment
https://forums.phpfreaks.com/topic/55380-session-time-out/#findComment-273693
Share on other sites

To implement this I will have to run a script every 5 or 10 min. to check who is not active and reset the 'Logged' variable. Is there a possibility like with SESSION? that if a user closes window the SESSION expires or timeout which will trigger an event?

 

thank you!

Link to comment
https://forums.phpfreaks.com/topic/55380-session-time-out/#findComment-273733
Share on other sites

You don't have to have a script running every 15 minutes (aka cron job) although that would be the best option. The method below works although if there is only one person logged in if they close the browser they will appear to stay logged in.

 

First, have a table caled something like "usersonline" and have fields like this:

username varchar(20)

logoff int(10) unsigned

 

Every script where you need to check the user is logged in or not have an include that does the following:

1. Check if the session var is empty - if yes redirect to the login script

2.

$kickTime=time()-600; //10 SECONDS
mysql_query("DELETE FROM `usersonline` WHERE `logoff`<'".$kickTime."'");

3. Check usersonline table for the user

4. If they were just deleted from the usersonline table:

4a. Get data from main users table

4b. Insert new user data into usersonline table

eg. INSERT INTO `users_online` (`username`,`logoff`,`level`) VALUES ('".$username."','".time()."')"

Link to comment
https://forums.phpfreaks.com/topic/55380-session-time-out/#findComment-273780
Share on other sites

Personally, i think you're far better off using Nhoj's method which simply uses a last active time to determine wether or not someone is online. By doing it with another table with users online, you'll just be adding to the amount of queries and processing that needs to be done.

Link to comment
https://forums.phpfreaks.com/topic/55380-session-time-out/#findComment-273785
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.