简体   繁体   English

无法设置Cookie,会话有效

[英]Cookies won't set, session works

I'm sorry for the small amount of information, but there is not much else I can give you. 很抱歉,我提供的信息很少,但是我能给您的信息不多。 The problem is, when trying to extend my login function to add an option for staying logged in, the cookies won't set. 问题是,当尝试扩展我的登录功能以添加用于保持登录状态的选项时,将不会设置cookie。 The session itself works (so there are no blank characters or something). 会话本身可以工作(因此没有空白字符或其他内容)。 I get no error messages, the cookie just doesn't set (tested with opera/firefox). 我没有收到任何错误消息,只是未设置Cookie(已通过Opera / firefox测试)。 After being puzzled for over an hour, i decided to ask it here. 经过一个多小时的困惑,我决定在这里问。 Below is the login function. 下面是登录功能。 Further down you will find how it's called. 在更下方,您将找到它的调用方式。 Don't worry about the password in the cookie, it's hashed. 不用担心cookie中的密码,它是经过哈希处理的。 I tested if the code is in fact executed (placed an echo 'abc' before setcookie) which is the case. 我测试了是否确实执行了代码(在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
}

}

This is how the above function is called. 这就是上述函数的调用方式。

<?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;
}

Thanks a lot. 非常感谢。

Your cookie won't set because you're outputting HTML headers before the log in / cookie setting functionality happens. Cookie不会设置,因为在登录/ cookie设置功能发生之前,您正在输出HTML标头。

You can't pass any headers to the browser prior to creating the cookie. 创建Cookie之前,您无法将任何标头传递给浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM