Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

php logout script trouble

Featured Replies

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\">";
?>

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 by andyl

  • 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 =(

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.

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 :)

  • 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!!

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.

  • 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?

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?

  • Author

its the same exact code, u gave as an example the only thing i changed was the time to 60seconds for testing it

So after waiting 60 seconds and then reloading a page is the session still valid?

  • 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'

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.

  • 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();
?>

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.