duanchi1230 2012-03-10 23:30
浏览 35
已采纳

会话过期时的PHP +注销成员

I've a site where people login and a SESSION is created.

I have noticed that if you leave the site for long enough (not sure exact time frame) the session ends but the members is still in the site. They can still click and navigate around and I believe this has resulted in some meaningless data in the DB as SESSION variables like userID don't exist.

I was looking for advice around logging users out when the SESSION ends. I have looked at code like this - any better ideas?

 <?php if(!isset($_SESSION[]) {header(loginpage.php);}?>

Is there a better way to write the above code?

Where should this code be placed? Just on the navigation menu or really on any place a user can click?

Finally is there a way to understand when the SESSION naturally expires - is there a SESSION variable I can print to screen to see the timeleft etc?

thanks

  • 写回答

2条回答 默认 最新

  • duanqun7761 2012-03-10 23:37
    关注

    You need to validate the session, you already headed into that direction with your code, but it's not enough:

    <?php if(!isset($_SESSION[]) {header(loginpage.php);}?>
    

    It's not enough because $_SESSION[] exists automatically after the session is started (the session mechanism in PHP, see session_start).

    Instead, if you have saved the userID inside the session, check that one:

    isset($_SESSION['userID'])
    

    If the session really expired, it should not be set.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?