簡體   English   中英

無法設置Cookie,會話有效

[英]Cookies won't set, session works

很抱歉,我提供的信息很少,但是我能給您的信息不多。 問題是,當嘗試擴展我的登錄功能以添加用於保持登錄狀態的選項時,將不會設置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;
}

非常感謝。

Cookie不會設置,因為在登錄/ cookie設置功能發生之前,您正在輸出HTML標頭。

創建Cookie之前,您無法將任何標頭傳遞給瀏覽器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM