$_SESSION 变量没有通过,也不知道为什么?它设置然后没有通过 [英] $_SESSION variable not passing and no clue why? it sets then doesn't pass
问题描述
我有一个用 php 和 mysqli 编写的登录脚本.提交并成功验证后,它会重定向到 success.php 以编写会话.我的成功页面如下所示.
I have a login script coded in php and mysqli. upon submission and successful authentication it redirects to success.php to write sessions. my success page looks like this.
<?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 无法识别会话变量.
This page effectively redirects to my index.php page. However my index php does not recognize the session variable.
<?php
session_start();
// rest of code
print "ths is the session var".$_SESSION['partner_id'];
?>
即在文档类型声明之前的页面顶部.
that is at the top of the page before the doctype declaration.
在页面上我也有
<?php
if(isset($_SESSION['partner_id'])) {
print "This is the session id variable".$_SESSION['partner_id'];
}
else {
print "Session not set";
}
?>
顶部的支票打印为空白,底部的支票打印会话未设置....
The top check prints blank and the bottom one prints session not set....
我很困惑...
推荐答案
您需要在所有使用 $_SESSION 的页面上使用 session_start()
You need session_start() on all pages using $_SESSION
<?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'] 中有值吗?
Also...are you sure that $_GET['id'] has a value in it?
而且....您的表单使用的是 POST 还是 GET?
And....is your form using POST or GET?
我以前犯过那个愚蠢的错误,其中表单使用 POST,而我使用 GET 检索变量并感到沮丧.只是一种可能
Ive made that silly mistake before , where form was using POST and I was retrieving variable with GET and getting frustrated. Just a possibility
这篇关于$_SESSION 变量没有通过,也不知道为什么?它设置然后没有通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!