Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

session timeout due to inactivity [duplicate]

Tags:

php

I have created a script login.php and there I have created a session variable named logged_in

$_SESSION['logged_in'] = true;

I am unable to figure out a way to redirect to redirect to my logout.php after session expires due to inactivity. Also should I put the code that expire this session variable. I have Googled the bug and what it suggest is to tweak php.ini file in most of the articles. However I came across an article saying that it is not the best practice.

I found the following code on StackOverflow, yet I have no idea where to put it:-

<?php
  if ($_SESSION['timeout'] + 10 * 60 < time()) {
     // session timed out
  } else {
     // session ok
  }
?>

I would like to know the best way to redirect after session expire and suggestions for where to put the code.

Edit: I forgot to mention that I want to know how to manually set a time for the session to expire.

Thank you in advance

like image 527
surfine Avatar asked Mar 24 '13 06:03

surfine


People also ask

What is session inactivity timeout?

The session inactivity timeout setting represents the amount of time a user can be inactive before the user's session times out and closes. It only affects user browser sessions. You can set the values from 5 minutes to 60 minutes. This function has a default value of 30 minutes.

How do I stop a session from timing out?

I implemented a solution for this: The client will "ping" the server at intervals of less than the session timeout which will reset the session timer. This is known as the Heartbeat design pattern (I couldn't find a decent site/page to link to)... +1 not only because it's a good solution, but also for the groovy title.

What is the difference between session timeout and idle timeout?

Absolute session timeout is a recommended security feature, while idle session timeout is mainly a resource management feature. Absolute session timeout requires all Spotfire users to log in to the program again after the configured amount of time.

What causes a session timeout?

User has been inactive for more than the specified time and the session has timed out. User has been disconnected from the internet mid-session. User has logged in on a different machine while the initial session is still active. The session on the first machine will expire.


1 Answers

If you want to logout the user if they try to load a page when they've been inactive for too long, you should put this code at the top of every php file (before ANY other html tags):

if( $_SESSION['last_activity'] < time()-$_SESSION['expire_time'] ) { //have we expired?
    //redirect to logout.php
    header('Location: http://yoursite.com/logout.php'); //change yoursite.com to the name of you site!!
} else{ //if we haven't expired:
    $_SESSION['last_activity'] = time(); //this was the moment of last activity.
}

Also, put this code at the top of the page where you land when you've successfully logged in:

$_SESSION['logged_in'] = true; //set you've logged in
$_SESSION['last_activity'] = time(); //your last activity was now, having logged in.
$_SESSION['expire_time'] = 3*60*60; //expire time in seconds: three hours (you must change this)

On that page you don't have to include the checking code I gave you first.

By the way, don't forget to add <?php tags correctly!

like image 168
tomsmeding Avatar answered Oct 22 '22 08:10

tomsmeding