【发布时间】:2012-09-27 23:37:11
【问题描述】:
我很抱歉提供少量信息,但我无法为您提供更多信息。问题是,当尝试扩展我的登录功能以添加保持登录的选项时,cookie 不会设置。会话本身有效(因此没有空白字符或其他东西)。我没有收到错误消息,只是没有设置 cookie(用 opera/firefox 测试)。困惑了一个多小时后,我决定在这里问它。下面是登录功能。再往下你会发现它是如何被调用的。不用担心 cookie 中的密码,它是经过哈希处理的。我测试了代码是否真的被执行了(在 setcookie 之前放置了一个 echo 'abc')。
public function login($password,$stayloggedin) {
if ( is_null( $this->id ) ) trigger_error ( "user::login(): Attempt to login an user object that does not have its ID property set.", E_USER_ERROR );
if ($this->compare_password($password))
{
if ($stayloggedin)
{
setcookie("userid", $this->id, time()+3600);
setcookie("userid", $this->password, time()+3600);
}
session_start();
$_SESSION['user_id'] = $this->id;
$_SESSION['user_name'] = $this->name;
$_SESSION['user_nummer'] = $this->user_nummer;
return true;
}
else
{
return false; //wrong password
}
}
上面的函数就是这样调用的。
<?php
header('Content-type: text/html; charset=UTF-8');
require_once 'required_script.php';
if (isset($_GET['login']))
{
require_once CLASS_PATH.'user.class.php';
$user_logging_in=new user();
$user_logging_in->load_from_name($_POST['user_name']);
if (isset($user_logging_in->id))
{
if ($user_logging_in->login($_POST['user_pass'],$_POST['remember_me']=='true'))
{
echo '1';
}
else
{
echo '0';
}
}
else
{
echo '2';
}
die;
}
非常感谢。
【问题讨论】:
-
我会尝试将接下来的两个参数用于
setcookie -
谢谢,这实际上解决了问题。你能解释一下为什么吗?也请重新发布它作为答案,以便我可以给你适当的信用。
-
我现在明白为什么了。登录发生在 /backend 和普通目录中的 cookie 检查。所以cookie只为/backend设置。非常感谢:)
-
我认为可能是这样,cookie 有很多小事情可能会出错 :)
标签: php session cookies http-headers