PHP: session isn#39;t saving before header redirect(PHP:在标头重定向之前会话未保存)
问题描述
我已经阅读了这个问题的 php 手册,这似乎是一个很常见的问题,但我还没有找到解决方案.我将会话保存在数据库中.我的代码如下:
I have read through the php manual for this problem and it seems quite a common issue but i have yet to find a solution. I am saving sessions in a database. My code is as follows:
// session
$_SESSION['userID'] = $user->id;
header('Location: /subdirectory/index.php');
然后在 session_start()
之后的 index.php 顶部,我已经 var_dumped 全局 $_SESSION 并且用户 ID 不在其中.正如我所说,我浏览了 PHP 手册 (http://php.net/manual/en/function.session-write-close.php
) 并且没有 session_write_close
或session_regenerate_id(true)
为我工作.有人知道解决方案吗?
Then at the top of index.php after the session_start()
, i have var_dumped the $_SESSION global and the userID is not in there. As i said ive looked through the PHP manual (http://php.net/manual/en/function.session-write-close.php
) and neither session_write_close
or session_regenerate_id(true)
worked for me.
Does anybody know a solution?
我的文件顶部有 session_start().当我在标头重定向之前对会话全局进行 var_dump 时,我在其中看到了用户 ID,但在位于该脚本的子目录中的另一个文件中没有看到
I have session_start() at the top of my file. When i var_dump the session global before the header redirect, i see the userID in there, but not in the other file, which is in a subdirectory of this script
推荐答案
我知道这是一个老话题,但我找到了解决方案(对我来说).我在标题后面放了一个出口.
I know this is an old toppic but I found the solution (for me). I've put a exit after the header.
$_SESSION['session'] = 'this is a session';
header('location: apage.php');
exit;
这对我有用
这篇关于PHP:在标头重定向之前会话未保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP:在标头重定向之前会话未保存


基础教程推荐
- Libpuzzle 索引数百万张图片? 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01