sathees Posted June 13, 2007 Share Posted June 13, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/55380-session-time-out/ Share on other sites More sharing options...
Nhoj Posted June 13, 2007 Share Posted June 13, 2007 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.'; } Quote Link to comment https://forums.phpfreaks.com/topic/55380-session-time-out/#findComment-273693 Share on other sites More sharing options...
sathees Posted June 13, 2007 Author Share Posted June 13, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/55380-session-time-out/#findComment-273733 Share on other sites More sharing options...
Nhoj Posted June 13, 2007 Share Posted June 13, 2007 You could just drop the 'logged' variable all together and just use the last part of my post to show whether or not a user is logged in at all. There's no way to have a session post information when it's cleaned... Quote Link to comment https://forums.phpfreaks.com/topic/55380-session-time-out/#findComment-273761 Share on other sites More sharing options...
Yesideez Posted June 13, 2007 Share Posted June 13, 2007 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()."')" Quote Link to comment https://forums.phpfreaks.com/topic/55380-session-time-out/#findComment-273780 Share on other sites More sharing options...
GingerRobot Posted June 13, 2007 Share Posted June 13, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/55380-session-time-out/#findComment-273785 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.