[英]PHP session variables getting lost after a header redirect
I cannot figure this out. 我无法弄清楚。 Sometimes after the redirect (see code below), the session variables are lost.
有时在重定向之后(请参见下面的代码),会话变量会丢失。 Any ideas?
有任何想法吗?
Note the script is initially called with ?p=1&u=2&k=3. 请注意,脚本最初是使用?p = 1&u = 2&k = 3调用的。 As you can see, the script redirects to itself.
如您所见,脚本将重定向到其自身。 The session variables something are lost after the redirect.
会话变量在重定向后会丢失。
<?php
session_start();
if ((isset($_SESSION['p'])) and ($_SESSION['p'] != "")) {
// do something
} else {
$_SESSION['p'] = $_GET['p'];
$_SESSION['w'] = $_SERVER["HTTP_HOST"];
$_SESSION['u'] = $_GET['u'];
$_SESSION['k'] = $_GET['k'];
header("Location: http://".$_SESSION['w'].$_SERVER['PHP_SELF']."");
exit();
}
?>
Cheers 干杯
Copied and pasted your code and it works just fine for me. 复制并粘贴您的代码,对我来说效果很好。 Do you maybe have some spaces or whatever before your
<?php
-tag? 在
<?php
-tag之前是否可能有空格或其他内容?
I am not sure why it happens. 我不确定为什么会这样。
Probably you have some misconfiguration on your php.ini
file. 可能您的
php.ini
文件配置有php.ini
。
Or you don't have the right session.save_path
or permissions to write there. 否则,您没有正确的
session.save_path
或写权限。
But if the problem persists, try this way: 但是,如果问题仍然存在,请尝试以下方法:
<?php
session_start();
if (!$_SESSION['p']) {
$_SESSION['p'] = $_GET['p'];
$_SESSION['w'] = $_SERVER["HTTP_HOST"];
$_SESSION['u'] = $_GET['u'];
$_SESSION['k'] = $_GET['k'];
}
//code comes here
?>
In my opinion, this is the way things should be done. 我认为这是应该做的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.