Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Destroy session if not any action in 10 minutes

Is there any option to destroy a session if user does not perform any action in 10 minutes?

like image 237
Eli_Rozen Avatar asked Jan 29 '12 00:01

Eli_Rozen


People also ask

How can we destroy session after a specific period 10 5 minutes?

You can decide the timeout with a function. Then set a session variable called 'timeout' in the session Now put a condition of timeout. If satisfied, it will destroy your session.

How can destroy session after some time in PHP?

A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.

What is timeout in session in PHP?

The session timeout is used to set the time limit for the inactivity of the user. Suppose, if the session timeout limit is set to 60 seconds and the user is inactive for 60 seconds then the session of that user will be expired and the user will require to log in again to access the site.

Is session destroy when browser is closed?

Browsers deletes the session cookies when the browser is closed, if you close it normally and not only kills the process, so the session is permanently lost on the client side when the browser is closed.


2 Answers

Try setting the session timeout to 10 minutes.

ini_set('session.gc_maxlifetime',10);
like image 150
jmort253 Avatar answered Oct 03 '22 02:10

jmort253


session_start();

// 10 mins in seconds
$inactive = 600; 

$session_life = time() - $_SESSION['timeout'];

if($session_life > $inactive) {
   session_destroy();
   header("Location: logoutpage.php");
}

$_SESSION['timeout']=time();

The code above was taken from this particular page.

like image 27
ose Avatar answered Oct 03 '22 02:10

ose