February 27, 201016 yr Is there anyway i can auto update a field in the database if a user logs out by closing the window and ending the session? <?php session_start(); include_once "connect_to_mysql.php"; $id = $_SESSION['id']; mysql_query("UPDATE myMembers SET logged_in='N' WHERE id='$id'"); // Destroy sessions and cookies session_destroy(); print "<meta http-equiv=\"refresh\" content=\"0;url=http://www.netdateme.com/index.php\">"; ?>
February 27, 201016 yr Edit.. nevermind, my previous logic was ridiculous. If you set a cookie, you could use the cookie to set someone to logged out if their session is not set? Edited February 27, 201016 yr by andyl
February 27, 201016 yr Author Edit.. nevermind, my previous logic was ridiculous. If you set a cookie, you could use the cookie to set someone to logged out if their session is not set? how'd ya do that? im not sure about cookies =(
February 27, 201016 yr If you mean can you detect when someone closes their browser window, then the simple answer is no. The best thing to do is make your user sessions timeout after a pre-determined period of inactivity. For example: if (array_key_exists('accessed', $_SESSION)) { if (time() - $_SESSION['accessed'] > 60 * 30) { $id = $_SESSION['id']; mysql_query("UPDATE myMembers SET logged_in='N' WHERE id='$id'"); // session last accessed more than 30 minutes ago session_destroy(); $_SESSION = array(); print 'Your session has expired due to inactivity.'; exit(); } else { $_SESSION['accessed'] = time(); } } Although this will obviously only get run when someone makes a request for some resource on your server.
February 27, 201016 yr how'd ya do that? im not sure about cookies =( I haven't got the time to write out the code at the moment, but the psuedo-code would be: Set cookie upon login Do a check on each page load - if cookie exists but session doesn't, OR session does and cookie doesn't, then execute query using the user id stored in cookie or session
February 27, 201016 yr Author thanks The Gator, i was thinking of doing smth like that but do i have to put that code into everypage? as i have many pages? thanks andyl i will try to figure it out and i'd be back here if i cant =) cheers!!
February 27, 201016 yr thanks The Gator, i was thinking of doing smth like that but do i have to put that code into everypage? as i have many pages? Its probably a good idea to work it into the function that you use to secure your pages, or setup the session for your users. You'd want to call that from every (secured) page to prevent unwanted access to your site.
February 28, 201016 yr Author Its probably a good idea to work it into the function that you use to secure your pages, or setup the session for your users. You'd want to call that from every (secured) page to prevent unwanted access to your site. The Gator, it doesnt seem to work, i have tried putting it into my login script as well as all the pages?
February 28, 201016 yr The Gator, it doesnt seem to work, i have tried putting it into my login script as well as all the pages? I'd need more information than that to be able to help. What actually happens? What does your code look like?
February 28, 201016 yr Author its the same exact code, u gave as an example the only thing i changed was the time to 60seconds for testing it
February 28, 201016 yr So after waiting 60 seconds and then reloading a page is the session still valid?
February 28, 201016 yr Author You are reloading the page, aren't you? afetr 60 seconds, when i close my browser, the database does not updat accordingly, it still shows logged_in as 'Y'
March 1, 201016 yr afetr 60 seconds, when i close my browser, the database does not updat accordingly, it still shows logged_in as 'Y' The script will only execute on page load, so close your browser then open it again. Go to your website ... now check the database.
March 1, 201016 yr Author hi andyl, i have change dmy code to this and its working fine =) but it doesnt auto refresh the page, only when a user clicks on a different link does it refresh n log the user out, is there any way to auto redirect n log out instead of havin the user to click on a different link? <?php session_start(); // set timeout period in seconds $inactive = 60; // check to see if $_SESSION['timeout'] is set if(isset($_SESSION['timeout']) ) { $session_life = time() - $_SESSION['start']; if($session_life > $inactive){ $id = $_SESSION['id']; mysql_query("UPDATE myMembers SET logged_in='N' WHERE id='$id'"); // session last accessed more than 30 minutes ago session_destroy(); print "<meta http-equiv=\"refresh\" content=\"0;url=http://www.domain.com/index.php\">"; } } $_SESSION['timeout'] = time(); ?>
March 3, 201016 yr Unfortunately not because if the user closes their browser - no refresh can be made.
Create an account or sign in to comment