Page 1 of 1

session timeout in php

Posted: Wed Jan 14, 2009 3:04 am
by miyur
hi everyone,
needed ur help to know dat how can i timeout a session in php?
i tried with ini_set('session.gc_maxlifetime',2); but dats not helping me out.
alse i tried wid dis code

Code: Select all

 
function checkSessionTimeOut(){
     // set timeout period in seconds
     $inactive=2;
     // check to see if $_SESSION['timeout'] is set
     if(isset($_SESSION['timeout'])){
         $session_life = time() - $_SESSION['timeout'];
         if($session_life > $inactive){
            return 1;
         }else{
            $_SESSION['timeout'] = time();
            return 2;
         }
     }else{
         $_SESSION['timeout'] = time();
         return 3;
     }
}
 
thanks.

Re: session timeout in php

Posted: Thu Feb 12, 2009 8:07 am
by joel24
I doubt that you're still looking for a reply, but I stumbled across your post when trying to solve the problem myself,
Like you I tried the ini_set('session.gc_maxlifetime',2); - to no avail... And you're attempt to create a session timeout script inspired me to create a little one of my own...

First of all I have a login function... if its returns false then the login page is called, if its true it continues the page.

Code: Select all

//check if logged in.
function loggedIn()
{
    return isset($_SESSION['authorised']);
}
The login script creates a $_SESSION['authorised'] = true when log in is successful, it also creates $_SESSION['time'] = time(); to create the login time.
On each page reload I have PHP require the page which holds these functions and the login authentication etc...

The next part of the code checks if the current time minus login time is more than the desired timeout amount (I have 10minutes, 10*60) and if it is it will exit, end the session and redirect to the login page.

If the current time minus login time is less than 10minutes, it will reset the $_SESSION['time'] to the current time so that only 10minutes of inactivity will instigate a timeout.

Code: Select all

if (loggedIn()) {
 
    if ((time() - $_SESSION['time']) > 10*60) {
        include 'login.inc.php';
        $_SESSION = array();
        session_destroy();
        exit;
    } else {
        $_SESSION['time'] = time();
    }
}

Re: session timeout in php

Posted: Wed Apr 01, 2009 12:27 am
by miyur
hey.. thank for the reply.
i just worked out on this issue. now it's working fine.
Anyways, thanks again.