简体   繁体   English

PHP session 值未设置

[英]PHP session values not set

I'm having some strange issues with PHP session variables claiming to not be set.我在声称未设置 PHP session 变量时遇到了一些奇怪的问题。 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.我的网站有一个 3 步向导,我使用会话来存储用户在每个步骤中的选择。 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:为了启动向导,我使用了一个初始化脚本来确保所有旧的向导 session 数据都被清除 - 这个初始化脚本然后将用户重定向到步骤 1。例如:

// 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:然后在 step1.php 的顶部,我进行如下检查:

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.当用户提交 step1 表单时,它会被发回给自己进行验证。 If it passes, another redirect is done to step 2.如果通过,则执行另一个重定向到第 2 步。

Most of the time, this works fine.大多数情况下,这工作正常。 In fact, the init script always works and the step1 form always loads without a problem.事实上,init 脚本总是可以正常工作,并且 step1 表单总是可以毫无问题地加载。 But sometimes, after submitting the step 1 form, the 'Step1 not initialized' exception gets thrown.但有时,提交第 1 步表单后,会抛出“第 1 步未初始化”异常。 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 数据,我不认为这是由于 session 超时或垃圾收集 - 一些相关的 php.ini 值。

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谢谢,布赖恩

Make sure that every script that uses your sessions begins with session_start()确保使用会话的每个脚本都以session_start()开头

Did you remember to call session_start() before interacting with $_SESSION and also before any output has be sent to the browser (including any white space or blank lines before <?php ?您是否记得在与$_SESSION交互之前以及在任何 output 被发送到浏览器之前调用session_start() (包括<?php之前的任何空格或空行?

If not the entire session is empty, but just that variable/key, you can use this to track the reason:如果不是整个 session 是空的,而只是那个变量/键,您可以使用它来跟踪原因:

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. ...并且您将拥有一个文件名+行号可能将其写入临时文件而不是回显它以确保您不会错过重定向等内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM