tomcant Posted November 3, 2009 Share Posted November 3, 2009 Greetings! I recently started learning the in's and out's of the CakePHP framework and to test my ability I began writing this little forum-style website. If you like, you can check it out here: http://tomcant.co.uk/thebored I don't often run into problems and when I do I usually sort them out without too much hassle. However, I'v become quite stuck. Here's my issue: When a user logs in I update the `users' table in my database to show that the user is online. When the user clicks logout I update the table again to show that the user is no longer online. I have a Who's Online section that queries the database and displays all the names of the logged in users. Simple. However, if the user simply closes their browser/tab, obviously my little databse-updating-code doesn't run and the changes aren't reflected in the Who's Online section. I'v really rooted around for a solution but can't find anything suitable. The solution I'v come up with goes something like this: each time a logged in user refreshes his/her browser, update a database table to show when this activity occurred (a timestamp). Then have some script execute every 5 minutes or so checking which users have been active recently, setting the status of a user to offline if they have been idle. This works to a certain extent, but what if the user was just idle and didn't click logout? My script would mark them as offline and this isn't what we want. Please help, it would be most appreciated. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/180101-updating-a-database-when-a-logged-in-user-logs-out/ Share on other sites More sharing options...
joel24 Posted November 3, 2009 Share Posted November 3, 2009 ... you could have an automatic ajax script which sent a request to a PHP page every 30seconds or 1minute or so? and that would update the timestamp in the database... Quote Link to comment https://forums.phpfreaks.com/topic/180101-updating-a-database-when-a-logged-in-user-logs-out/#findComment-950126 Share on other sites More sharing options...
tomcant Posted November 3, 2009 Author Share Posted November 3, 2009 Thanks for the reply. Would you be able to elaborate on that a little? Perhaps you can explain how I would set that up...? Quote Link to comment https://forums.phpfreaks.com/topic/180101-updating-a-database-when-a-logged-in-user-logs-out/#findComment-950130 Share on other sites More sharing options...
joel24 Posted November 3, 2009 Share Posted November 3, 2009 you'd have a PHP page with a script like //security measures up here? i.e. if (isset($_SESSION['logged_in'])) { raghh } $user_id = $_SESSION['id']; $sql = @mysql_query("UPDATE users SET timestamp = UNIX_TIMESTAMP() WHERE userid = $user_id"); then i'd have a jquery script in the <head> of each page like so <script> jQuery(document).ready(function() { function pageLoad() { //ajax for automatic pageload, change the .php page to whatever you make $.post("yourNewPhpPage.php"); //call 30sec timeout setTimeout(pageLoad(), 30000); }; pageLoad(); }); </script> jquery isn't my specialty, but that *should* work... **EDIT** to run this you'll need to have the jquery file linked to your site... jquery.com Quote Link to comment https://forums.phpfreaks.com/topic/180101-updating-a-database-when-a-logged-in-user-logs-out/#findComment-950156 Share on other sites More sharing options...
tomcant Posted November 3, 2009 Author Share Posted November 3, 2009 I see. Could you just clear one thing up for me... what will be at `yourNewPhpPage.php' ? Thanks for your help. [EDIT]: Ok, I figured it out. Thanks again for your help. I'll post here if I have any further problems. Quote Link to comment https://forums.phpfreaks.com/topic/180101-updating-a-database-when-a-logged-in-user-logs-out/#findComment-950164 Share on other sites More sharing options...
joel24 Posted November 3, 2009 Share Posted November 3, 2009 yourNewPhpPage.php will be a simple php page with this code, you'll have to set up the $user_id variable so it gets the userid from the $_SESSION correctly... if you have it stored in there? //security measures up here? i.e. if (isset($_SESSION['logged_in'])) { raghh } $user_id = $_SESSION['id']; $sql = @mysql_query("UPDATE users SET timestamp = UNIX_TIMESTAMP() WHERE userid = $user_id"); Quote Link to comment https://forums.phpfreaks.com/topic/180101-updating-a-database-when-a-logged-in-user-logs-out/#findComment-950166 Share on other sites More sharing options...
tomcant Posted November 3, 2009 Author Share Posted November 3, 2009 Ok, this isn't working so clearly in my head anymore. Think of it like this: a user logs in and the database is updated with a new timestamp. The user then closes the browser. How is my site going to update the database to show that the user logged out? Quote Link to comment https://forums.phpfreaks.com/topic/180101-updating-a-database-when-a-logged-in-user-logs-out/#findComment-950177 Share on other sites More sharing options...
joel24 Posted November 4, 2009 Share Posted November 4, 2009 the ajax keeps updating the db every 30 seconds, so if the user has logged out the timestamp will be older than 30 seconds. and on top of that i'd also put a bit in my logout script saying $sql = @mysql_query("UPDATE users SET loggedIn = 0 WHERE userID = $userID"); and then for the script to see if users are logged in $userID = whatever; $sql = @mysql_query("SELECT loggedIn, timestamp FROM users WHERE userID = $userID"); $userDetails = mysql_fetch_array($sql); if ($userDetails['loggedIn'] == 1 && ((time() - $userdetails['timestamp']) < 30)) { //user is logged in, echo logged in image or whatever you want to do } else { //user is logged off } Quote Link to comment https://forums.phpfreaks.com/topic/180101-updating-a-database-when-a-logged-in-user-logs-out/#findComment-950655 Share on other sites More sharing options...
tomcant Posted November 4, 2009 Author Share Posted November 4, 2009 Excellent. Thanks, Joel. Quote Link to comment https://forums.phpfreaks.com/topic/180101-updating-a-database-when-a-logged-in-user-logs-out/#findComment-950833 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.