PHP:session-isn';标题重定向前不保存

PHP:session-isn';标题重定向前不保存,php,session,header,sessionid,Php,Session,Header,Sessionid,我已经阅读了php手册中关于这个问题的内容,这似乎是一个很常见的问题,但我还没有找到解决方案。我正在数据库中保存会话。 我的代码如下: // session $_SESSION['userID'] = $user->id; header('Location: /subdirectory/index.php'); ... //Users is validated and redirected. $_SESSION['client'] = $client; header('l

我已经阅读了php手册中关于这个问题的内容,这似乎是一个很常见的问题,但我还没有找到解决方案。我正在数据库中保存会话。 我的代码如下:

// session
$_SESSION['userID'] = $user->id;        
header('Location: /subdirectory/index.php');
...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
然后在
session\u start()
之后的index.php顶部,我已将$u session global转储到var\u,但用户ID不在其中。正如我所说,我浏览了PHP手册(
http://php.net/manual/en/function.session-write-close.php
)和
会话写入\u关闭
会话重新生成\u id(true)
对我都不起作用。 有人知道解决办法吗

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();

编辑:我的文件顶部有session_start()。当我在头重定向之前对会话全局进行var_转储时,我在其中看到了userID,但在另一个文件中看不到,该文件位于该脚本的子目录中

您有会话_start()吗;在上面

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
未经测试,但您不能这样做:

session_start();
$_SESSION['userID'] = $user->id;
if( $_SESSION['userID'] == $user->id )
{  
    header('Location: /index.php');
}
...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();

我以前从未遇到过这个问题,有趣的

userID没有任何关键字状态

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
对我来说,唯一的原因是$\u会话['userID']在某处被覆盖或删除

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
确保在要添加/访问会话的所有文件中使用会话->开始()

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
一件重要的事情(可能不适用于您的情况)是,如果会话是使用cookie处理的,那么cookie只能在特定目录和该目录下的子目录下访问。 无论如何,在您的情况下,子目录将有权访问会话。

@Matt(还不能评论…):如果:
...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
a) 它出现在重定向之前的会话中
b) 其他的钥匙也有用

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();

80%的情况下,问题在于注册全局变量,并在某个地方使用同名变量$userID(另外19%只是在不希望的地方进行覆盖,1%无法在重定向和过时数据之前写入/锁定会话,在这种情况下,您可以在重定向之前尝试会话写入关闭()。不用说register\u globals应该关闭:P

确保两个页面的php版本相同

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();

(php5、php4有时有不同的会话路径)

我没有听说过这个问题,但我没有经常使用会话

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
对于会话,您必须做一些事情并进行一些设置:

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
  • 在客户端启用cookies
  • 会话_start(),在发生任何事情之前
  • 确保不要破坏会话(除非他们想注销)
  • PHP会话id必须相同(与Cookie相关)
另一个问题可能是$user->id返回对下一页上不存在的对象的引用。很可能不会,但要确保

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
如果我看到你的代码,我可以帮你更多。但在调试时,请使用session_id()检查会话密钥,并确保其相同。如果你能试试的话,告诉我我可以继续帮忙

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();

我也很想知道,当我回到会议中时,这将如何结束。

我知道这是一个古老的toppic,但我(为我)找到了解决方案。
...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
我在头球后放了一个出口

$_SESSION['session'] = 'this is a session';
header('location: apage.php');
exit;
...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();

这对我很有效

我最近也有同样的问题。我正在为学校编写一个定制的MVC网站,正如大家所说的,start_session()必须在第一行代码中编写

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
我的问题是“session_start()”的位置。它必须是全局控制器的第一行,而不是视图的第一行$_无法在控制器的文件中访问会话,因为它仅在服务器呈现视图时启动

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
然后,我在header('location:xxx.php')调用之后使用session_write_close()为下一个请求保留会话变量

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
例:

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
globalController.php:

//First line
session_start();
require_once('Model/Database.php');
require_once('Model/Shop/Client.php');
...
...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
logonController.php:

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();

希望它能解决你的问题。

这让人非常恼火,但我终于找到了解决办法

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
config.php我有: 包括“session.php”

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
在session.php的顶部,我有: 会话_start()

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
通过将session_start()移动到config.php文件的顶部,viola

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();

问题解决了

您应该在使用会话数组之前启动会话。

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
PHP代码
会话_start()
$\会话['userID']=$user->id


标题('Location:/subdirectory/index.php')

使用
exit
强制终止脚本的另一个选择是使用
session\u write\u close
强制将更改写入会话存储

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
但是,如果脚本正确终止,则不应发生这种情况

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
结束当前会话并存储会话数据

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();
会话数据通常是在脚本终止后存储的,没有 需要调用session_write_close(),但因为session数据被锁定 为了防止并发写入,一个会话上只能运行一个脚本 随时都可以。将框架集与会话一起使用时,您将 由于此锁定,体验一个接一个的框架加载。你可以 通过按以下方式结束会话,减少加载所有帧所需的时间 一旦会话变量的所有更改完成

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();

在我的例子中,这只发生在使用
Xdebug
进行调试期间,当时我多次触发同一脚本,因此多个进程试图操纵同一会话。不知何故,该会话无法再解锁。

您是在哪里执行重定向的
session\u start()
吗?您确定设置了$user->id吗?您好,请查看我为@ahmet2106的post留下的注释什么是register\u globals state?是的,我是这样做的。当我在头重定向之前var_转储会话全局时,它在那里,而不是当我转到原始文件的子文件夹中的新文件时script@Matt有趣的是,它真的很有趣,因为应该设置会话,可能其他代码正在删除您的会话,您确定没有其他地方是会话吗?奇怪的是,我将$\u会话['userID']更改为$\u会话['uid'],并按预期工作。我不知道为什么这两个大写字母是
...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();