Jump to content

session logout


searls03

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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'");
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.