dqzd92796 2013-01-11 15:01
浏览 24
已采纳

当会话在我们仍然查看页面时到期时,我们应该怎么做?

I have a page which contains $_SESSION variables. Now after a certain amount of time, all the sessions will expire which is fair enough. But the problem is that suddenly we get undefined notices on our $_SESSION variables as they are now expired.

So in this situation what is the best thing to do when a session expires. DO we just include a the E ^ NOTICE code in order to not show notices (afterwe know the page is fully complete with no errors), or are we expected to destroy a session using session_destroy() on the page, or is there other ways of not showing these undefined errors to the users who are viewing these pages on the browser and may view the page when the session has expired.

Below is my example code I have on how I have $_SESSION code displayed:

 <?php

    ini_set('session.gc_maxlifetime',12*60*60); //12 hours until session expires
    ini_set('session.gc_divisor', '1');
    ini_set('session.gc_probability', '1');
    ini_set('session.cookie_lifetime', '0');
    require_once 'init.php'; 

    session_start();  //starts sessions

    include('member.php');  //this php script also contains sessions


    if (isset($_POST['id'])) {

    $_SESSION['id'] = $_POST['id'];

    }


    if(isset($_POST['sessionNum'])){
                //Declare my counter for the first time

                $_SESSION['initial_count'] = $_POST['sessionNum'];
                $_SESSION['sessionNum'] = intval($_POST['sessionNum']);
                $_SESSION['sessionCount'] = 1;

        }

    if (isset($_POST['submitDetails']) && $_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
        $_SESSION['sessionCount']++;
    }


    $sessionMinus =  $_SESSION['sessionCount'];


    if (isset($_POST['textQuestion'])) {

    $_SESSION['textQuestion'] = $_POST['textQuestion'];

    }

    if (isset($_POST['textMarks'])) {

    $_SESSION['textMarks'] = $_POST['textMarks'];

    }

    if (isset($_POST['totalWeight'])) {

    $_SESSION['totalWeight'] = $_POST['totalWeight'];

    }

    if ($sessionMinus == $_SESSION['initial_count']){ 

        $action = 'individualmarks.php'; 

    }elseif($sessionMinus != $_SESSION['initial_count']){ 

        $action = $_SERVER['PHP_SELF']; 

    }

    ?>
  • 写回答

3条回答 默认 最新

  • dongpao1873 2013-01-11 15:05
    关注

    Check for the presence of a particular sentinel value. e.g. if whatever you're doing with the session is for a login-protected system, then check the value of isset($_SESSION['loggedIn']) && ($_SESSION['loggedIn'] == true).

    If the sentinel value(s) are not present, then something's happened to the session, and you should redirect the user elsewhere to restore things to the way they should be (e.g. show the login page).

    Since you can't know in advance what page a "slow" user will hit, you have to assume the session can vanish at any time, and do the sentinel checks on EVERY page.

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

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路