简体   繁体   English

会话变量未从$ _REQUEST保存

[英]Session variable not being saved from $_REQUEST

I think i'm missing something obvious. 我想我缺少明显的东西。 I have a session started at the very top of my page. 我的会话从页面的顶部开始。 Below that i have the following code. 在此之下,我有以下代码。 The var dump out puts "one" when it is displayed from the requested page. 从请求的页面显示时,var dump out放置"one" After refresh the var dump out puts NULL . 刷新后,var dump out放入NULL Why is this not getting saved? 为什么没有保存?

if($_REQUEST["page"] == 1) {
    $_SESSION["one"] = true;
}

var_dump($_SESSION["one"]);

First, you need to ensure you start the session before attempting to use it. 首先,您需要确保在尝试使用会话之前启动会话。 Secondly, it is recommended that you specify either POST or GET instead of generally using REQUEST. 其次,建议您指定POST或GET而不是通常使用REQUEST。 If you want to support either GET or POST, you might do something like this: 如果要支持GET或POST,则可以执行以下操作:

// Begin Session Management
session_start();

// Check both GET and POST for the parameter
if($_GET['page'] == 1 || $_POST['page'] ) {
    // Modify the session 
    $_SESSION["one"] = true;
}

// See what we ended up with in the session.
var_dump($_SESSION["one"]);

This works for me, but I'm using memcache as my session session handler. 这对我有用,但是我将memcache用作会话会话处理程序。 Verify your own session handler in php.ini, and ensure that the session handler is working properly. 在php.ini中验证您自己的会话处理程序,并确保该会话处理程序正常运行。 Also, ensure you are closing the session properly if you are redirecting, setting a new location, or exiting in unusual ways. 另外,如果您要重定向,设置新位置或以异常方式退出,请确保正确关闭会话。

If you have session_start() at the top of your page, as you claim, then your code should look something like this: 如您所声称的,如果页面顶部有session_start() ,那么您的代码应如下所示:

session_start();

if($_REQUEST["page"] == 1) {
    $_SESSION["one"] = true;
}

var_dump($_SESSION["one"]);

This should 100% work, no question. 毫无疑问,这应该100%有效。 There IS something else stopping this from working in your code that you have not supplied. 还有其他一些事情可以阻止它在您未提供的代码中工作。 My first guess would be a session destroy of some kind. 我的第一个猜测将是某种会话破坏。

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

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