PHP:Cookie 未取消设置
<?
if($_POST["Login"])
{
if (GetRightPassword($_POST["emaillogin"],$_POST["passwordlogin"]))
{
$_SESSION["email"] = $_POST["emaillogin"];
$_SESSION["password"] = $_POST["passwordlogin"];
echo "Keeping logged in: ".$_POST["keeploggedin"];
if ($_POST["keeploggedin"])
{
setcookie("email", $_POST["emaillogin"], time()+60*60*24*365);
setcookie("password", $_POST["passwordlogin"], time()+60*60*24*365);
}
}
else
{
echo "Invalid username/password!";
}
}
if($_POST["Logout"])
{
$_SESSION["email"] = null;
$_SESSION["password"] = null;
setcookie("email", "", time()-900000);
setcookie("password", "", time()-900000);
}
echo $_COOKIE["email"];
?>
这是唯一写入 cookie 或会话的代码(据我所知,我至少在 6 个月前编写了它,但我很确定没有更多代码了)。
当我单击注销时,它会将会话变量清空,因此当页面加载时,我会注销 - 再次更改页面或刷新,然后我会再次登录。
有什么想法吗?当我更改页面时,没有发送登录信息,所以我不知道为什么。
如果有帮助,请 echo $_COOKIE["email"];即使只是将其设置为“”,该行也会回显您的电子邮件。
编辑
我刚刚找到了更多与此相关的代码。
此代码在该代码之前运行。
if(isset($_COOKIE["email"]))
{
$_SESSION["email"] = $_COOKIE["email"];
$_SESSION["password"] = $_COOKIE["password"];
}
<?
if($_POST["Login"])
{
if (GetRightPassword($_POST["emaillogin"],$_POST["passwordlogin"]))
{
$_SESSION["email"] = $_POST["emaillogin"];
$_SESSION["password"] = $_POST["passwordlogin"];
echo "Keeping logged in: ".$_POST["keeploggedin"];
if ($_POST["keeploggedin"])
{
setcookie("email", $_POST["emaillogin"], time()+60*60*24*365);
setcookie("password", $_POST["passwordlogin"], time()+60*60*24*365);
}
}
else
{
echo "Invalid username/password!";
}
}
if($_POST["Logout"])
{
$_SESSION["email"] = null;
$_SESSION["password"] = null;
setcookie("email", "", time()-900000);
setcookie("password", "", time()-900000);
}
echo $_COOKIE["email"];
?>
That's the only code (As far as I can find, I coded it 6 months ago minimum but I'm prety sure there's no more) that writes to cookies or session.
When I click logout, it nulls the session variables, so when the page loads, I'm logged off- Change page again or refresh though, and I'm logged back in again.
Any ideas why? Login isn't being sent when I change page, so I have no idea why.
If it helps, the echo $_COOKIE["email"]; line echos your email even when it's just set it to "".
Edit
I just found more code related to this.
This code is ran before that code.
if(isset($_COOKIE["email"]))
{
$_SESSION["email"] = $_COOKIE["email"];
$_SESSION["password"] = $_COOKIE["password"];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在所有 cookie 中设置路径和域。这可以确保它不是像带有和不带
www
的东西。检查文档如何执行此操作: http://nl2.php.net/setcookie
这可能是你的问题的根本原因
set in all your cookies the path and domain. This ensures it's not something like a with and withoud
www
.Check the documentation how to do this: http://nl2.php.net/setcookie
This might be the root cause for your problem