我有一个登录脚本在php和mysqli编码。在提交和成功验证后,它将重定向到success.php来编写会话。我的成功页面是这样的。
<?php
/**
* Set cookies here if/as needed.
* Set session data as needed. DO NOT store user's password in
* cookies or sessions!
* Redirect the user if/as required.
*/
$id = $_GET['id'];
$_SESSION['partner_id'] = $id;
$_SESSION['authenticated'] = TRUE;
if (isset($_SESSION['partner_id'])) {
echo("<script>
<!--
location.replace(index.php);
-->
</script>");
}
else {
print "Session partner id not set";
}
?>
这个页面有效地重定向到我的index.php页面。然而,我的索引php不识别会话变量。
<?php
session_start();
// rest of code
print "ths is the session var".$_SESSION['partner_id'];
?>
在文档类型声明之前的页面顶部。
页面上还有
<?php
if(isset($_SESSION['partner_id'])) {
print "This is the session id variable".$_SESSION['partner_id'];
}
else {
print "Session not set";
}
?>
上面的检查显示空白,下面的检查显示会话未设置....
您需要在使用$_SESSION
的所有页面上使用session_start() <?php
/**
* Set cookies here if/as needed.
* Set session data as needed. DO NOT store user's password in
* cookies or sessions!
* Redirect the user if/as required.
*/
session_start();
$id = $_GET['id'];
$_SESSION['partner_id'] = $id;
$_SESSION['authenticated'] = TRUE;
也……你确定$_GET['id']里面有一个值吗?
…您的表单是使用POST还是GET?
我以前犯了那个愚蠢的错误,在那里表单使用POST,我正在检索变量与GET和沮丧。只是一种可能性
使用session_start();在PHP中使用$_SESSION变量之前。session_start ();函数用于初始化会话进程。
success.php应该是。
<?php
/**
* Set cookies here if/as needed.
* Set session data as needed. DO NOT store user's password in
* cookies or sessions!
* Redirect the user if/as required.
*/
session_start();
$id = $_GET['id'];
$_SESSION['partner_id'] = $id;
$_SESSION['authenticated'] = TRUE;
if (isset($_SESSION['partner_id'])) {
echo("<script>
<!--
location.replace(index.php);
-->
</script>");
}
else {
print "Session partner id not set";
}
?>
初始文件不包含session_start()。然而,由于某种原因,现在没有它也能工作。
我正在考虑缓存或服务器延迟问题也
如果你不想在每个页面的开始都写session_start(),你可以通过在php.ini文件中设置session.auto_start = 1
来跳过它,它会在请求页面自动启动会话。参考PHP Session Configuration Documentation