bsanneh
bsanneh

Reputation: 521

Disconnect a user after 5mins

Can somebody please help me on how to disconnect a user (using sessions) after 5mins regardless of being idle or active.thank you am coding in php and mysql as the database.

Upvotes: 1

Views: 1286

Answers (3)

Ali Rankine
Ali Rankine

Reputation: 165

When the user logs on, you could register a session variable for the time, every time a page is loaded, check if 5 minutes has elapsed since logging on. For example:

<?php
session_start();
if($_SESSION["loggedin"] && ($_SESSION["timer"]<(time()-(5*60)))) {
logout();

}


function login() {
//do login stuff
$_SESSION["timer"]=time();
}

function logout() {
//do logout stuff
}

?>

Upvotes: 3

Florian
Florian

Reputation: 3171

You can save a timestamp of the last action in the $_SESSION array and compare $timestamp + 300 with the current time.

Just setting the garbage collector lifetime to a low value won't work because the garbace collector doesn't run on every page request.

Upvotes: 1

Sarfraz
Sarfraz

Reputation: 382706

Use session.gc-maxlifetime:

ini_set('session.gc-maxlifetime', 60*5);

Once above line is read by the php interpreter, your session will expire after 5 minutes.

Upvotes: 2

Related Questions