开发者

Can't make sessions work properly

I'm trying to control if the session is started, and doing the redirects according to it, and also checking the session time, if it's expired redirect, if not - continue.

I have two pages:

in one page, where the user initially enters in the session isn't set yet, I have this code:

session_start(); 
$_SESSION['timeout'] = time(); 
header('Location: index.php');

on the second page I have this:

$inactive = 600;
if(isset($_SESSION['timeout']) ) {
    $session_life = time() - $_SESSION['timeout'];
    if($session_life > $inactive)
        { session_destroy(); header("Location: intro.php");开发者_高级运维 }
} 
else { header("Location: intro.php"); }

it still brings me to the first page (intro.php)

What's wrong with my code here?

and by the way... instead of redirecting when $session_life > $inactive I'd like to update the session, so that session never expires. Any tips?


You have to call session_start() in every page you plan to use sessions.

So just add a session_start() to your second page (remember to do that prior to echoing any output) and you should be fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜