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
https://forums.phpfreaks.com/topic/243523-session-logout/
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
https://forums.phpfreaks.com/topic/243523-session-logout/#findComment-1250449
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
https://forums.phpfreaks.com/topic/243523-session-logout/#findComment-1250455
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
https://forums.phpfreaks.com/topic/243523-session-logout/#findComment-1250476
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.