Jump to content

PHP timeout and redirect


wilna

Recommended Posts

Hi, I have been searching the forums & google but still am strugling.  If the user has been inactive for 10 mins I want the page to automatically throw them out.  At this stage my script throws them out but only if they have been inactive for 10 mins and then click on something.  My code looks like this:

 

// 10 mins in seconds

$inactive = 600;

 

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

if($session_life > $inactive)

{ session_destroy(); header("Location: http://www.mydomain.com/login_new1.htm"); }

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

Link to comment
https://forums.phpfreaks.com/topic/196573-php-timeout-and-redirect/
Share on other sites

// 10 mins in seconds

$inactive = 600;

 

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

if($session_life > $inactive)

{ session_destroy(); header("Location: http://www.mydomain.com/login_new1.htm"); }

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

 

 

to

 

 

// 10 mins in seconds

$inactive = 600;

 

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

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

if($session_life > $inactive)

{ session_destroy(); header("Location: http://www.mydomain.com/login_new1.htm"); }

 

?

 

 

 

 

modify:

 

I didn't pay attention that you said the code isss working already. It just seemed to me that you'd have to set your $_session['timeout'] before you can check it in $session_life. Anyway, the reason why it works when they click on something is because the php starts working again. When a visitor logs in currently, the php checks your code on the first load and since the > condition isn't met it skips it and never come back again until the php is reactivated. I'm not sure on this, but I'd guess you need a loop running. Something like:

 

$i=0;

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

$_session['timeout']=time(); //or these two visa versa like i had it

If($i<10){

$inactive = 600;

if($session_life > $inactive)

{ session_destroy(); header("Location: http://www.mydomain.com/login_new1.htm"); }

$i++;

$i = 0;

}

 

This would keep the loop on going, constantly checking the time. Totally just a guess tho.

 

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.