dongtang1909 2011-06-07 18:45
浏览 10
已采纳

PHP会话值未设置

I'm having some strange issues with PHP session variables claiming to not be set. I'm only encountering this in one particular situation:

My site has a 3-step wizard and I use sessions to store the user's selections on each step. To start the wizard, I use an init script that ensures any old wizard session data is wiped out - this init script then redirects the user to step 1. For example:

// Initialize wizard session and send user to step 1
$_SESSION['wizard'] = array();
$_SESSION['wizard']['step1'] = TRUE;
session_write_close();
header('Location: http://mysite.com/wizard/step1.php');

Then at the top of step1.php, I do a check like:

if (!isset($_SESSION['wizard']['step1'])) 
    throw new Exception('Step1 not initialized');

When the user submits the step1 form, it is posted back to itself for validation. If it passes, another redirect is done to step 2.

Most of the time, this works fine. In fact, the init script always works and the step1 form always loads without a problem. But sometimes, after submitting the step 1 form, the 'Step1 not initialized' exception gets thrown. I don't see how the initial load could pass the check but the form post fail it moments later. Especially considering this problem happens infrequently and most of the time there are no problems at all.

I am using a database to store my session data and I don't think this is due to session timeouts or garbage collection - some related php.ini values:

session.use_cookies = 1
session.cookie_lifetime = 0
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 86400

Does anyone know what could be causing such a problem? Any insight would be greatly appreciated.

Thanks, Brian

  • 写回答

3条回答 默认 最新

  • duanrebo3559 2011-06-07 19:43
    关注

    If not the entire session is empty, but just that variable/key, you can use this to track the reason:

    class foo extends ArrayObject{
        function __destruct(){
            echo 'dying:';
            debug_print_backtrace();
        }
    }
    session_start();
    $_SESSION['wizard'] = new foo();
    //array access is still possible
    $_SESSION['wizard']['foz'] = 1234;
    //reading it like an array also
    echo $_SESSION['wizard']['foz'];
    //on normal completion, it also gets called, the backtrace would be:
    //dying:#0  foo->__destruct()
    //^ ignore those
    
    //on overwriting / deleting values, like for instance this by accident:
    $_SESSION['wizard'] = array();
    //the backtrace is something like:
    //dying:#0  foo->__destruct() called at [filename:linenumber]
    

    ... and you'll have a filename+linenumber Possibly write it to a temporary file rather then echo'ing it to make sure you don't miss stuff on redirects etc.

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

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上