searls03 Posted August 1, 2011 Share Posted August 1, 2011 ok,so I use sessions in my system. my question is how can I make it so that when a session expires, it runs a php code. I have a chat system set up where when a user logs in, it sets a row in the database called loggedin to 1 which which means they are available, when they logout, it sets it to 0. I need to know how to make it so that when the session expires, such as they just closed the window and never logged out, it will set loggedin to = 0, so that they are not available for chat. does this make sense? is there a better way to do this? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 1, 2011 Share Posted August 1, 2011 store the timestamp with every action they perform (whenever they click on something or send a message). Quote Link to comment Share on other sites More sharing options...
searls03 Posted August 1, 2011 Author Share Posted August 1, 2011 how do I do that? I have never tried that before. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 1, 2011 Share Posted August 1, 2011 if you use time() it will give you a timestamp in seconds. (number of seconds since 1st Jan 1970 to be more exact) Store that in you database whenever a user interacts, then all you need to do is search through the database and set the field to 0 on all timestamps that are greater than 2 minutes (whatever you want). since it's all done in seconds, 2 minutes = 120 seconds. Quote Link to comment Share on other sites More sharing options...
searls03 Posted August 1, 2011 Author Share Posted August 1, 2011 but what would be the code I use to do something like this? like i said, I have never tried this before. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 1, 2011 Share Posted August 1, 2011 Something in the lines of: // set time of last activity $userTime = time(); mysql_query("update `table` set where `activityTime` = '$userTime'",$conn); // force logout to those inactive for over 2 minutes $inactivityTime = time() - 120; // 2 minutes mysql_query("update `table` set `loginStatus` = '0' where `activityTime` < '$inactivityTime' ",$conn); Quote Link to comment Share on other sites More sharing options...
searls03 Posted August 1, 2011 Author Share Posted August 1, 2011 my fault, how do I make it so that the time submits when an action is made? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 1, 2011 Share Posted August 1, 2011 with the code I gave you above. Quote Link to comment Share on other sites More sharing options...
searls03 Posted August 1, 2011 Author Share Posted August 1, 2011 sorry, I guess my question is, how does the system know when to submit? is that what time() is? please explain. sorry. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 1, 2011 Share Posted August 1, 2011 if you use time() it will give you a timestamp in seconds. (number of seconds since 1st Jan 1970 to be more exact) Quote Link to comment Share on other sites More sharing options...
searls03 Posted August 1, 2011 Author Share Posted August 1, 2011 but how does the site know to lets say the user went to a new page? how does the site know this and to put a timestamp in? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 1, 2011 Share Posted August 1, 2011 what happens when a user goes to a new page? that page's PHP code executes and return something to the user, right? Just put the code in that page, or script, whatever you're calling. Quote Link to comment Share on other sites More sharing options...
searls03 Posted August 1, 2011 Author Share Posted August 1, 2011 ok, i think I get it now. Quote Link to comment Share on other sites More sharing options...
searls03 Posted August 1, 2011 Author Share Posted August 1, 2011 if the page isnt refreshed, then how does the system know it has been more that 120 secs? this is what I have come up with, I am pretty sure it wont work though: <?php session_start(); // Must start session first thing /* Created By Adam Khoury @ www.flashbuilding.com -----------------------June 20, 2008----------------------- */include_once "connect_to_mysql_1.php"; // Here we run a login check if (!isset($_SESSION['username'])) { // 2 minutes mysql_query("update `sessions` set `loggedin` = '0' where `username`= ".($_SESSION['username'])." "); echo 'Please <a href="/login.php">log in</a> to access your account'; exit(); } //Connect to the database through our include // Place Session variable 'id' into local variable $username1 = $_SESSION['username']; $name1 = $_SESSION['name']; ?> <?php // set time of last activity $userTime = time(); mysql_query("update `sessions` set `activity` = '$userTime' where username='$username1'"); ?> Quote Link to comment 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.