0

我有一个基于CodeIgniter. 我试图在我的控制器功能中设置 cookie。这是我的代码的一部分

private function login_core($username, $user_pass){
    //Get value from database
    $this->load->model('user_model');
    if($this->user_model->init($username) == 1){
        $pass = $this->user_model->getPassword();
        $this->e['db_pass'] = $pass; 
        //Now encrypt th real password 
        //Make thisL
         $e_p = $username. $user_pass . $this->user_model->getEmail();
         $e_p = sha1($e_p);
         $this->e['act_pass'] = $e_p;
         //Whenever the email is changed, update the password. (Require password to change the email)
        $this->load->library('encrypt');
        $this->load->library('xsecurity');

        //cookie not set :( . And i don't know the reason
        if($pass == $e_p){
            $this->xsecurity->SetInfoLocal($e_p);
            setcookie('y',$this->encrypt->encode($username, $_SERVER['HTTP_USER_AGENT']),60*60*24*3);
            return true;
        } else {
            return false; //false'
        }

    } else {
        return false;
    }
}


public function login(){
    if(!isset($_POST['user'], $_POST['pass'])){ 
        $this->load->view('header');
        $this->load->view('content/errorError');
        $this->load->view('footer');
        return false;
    }

    $user = $_POST['user'];
    $pass = $_POST['pass'];


    if(self::login_core($user, $pass)){
        $d['a'] = $this->e;
        $this->load->view('header', $d);
        if(isset($_GET['redirect_to'])) { 
            ob_start();
            header('location:'. $_GET['redirect_to']);
            return true;
        }
        $data['userloggedOn'] = true;
        $this->load->view('main', $data);
    }
    else
    {
        $data['userloggedOn'] = false;
        $this->load->view('main', $this->e);
    }

    $this->load->view('footer');
}

用户指向通过表单登录,并且在我看来,在设置 cookie(用于登录页面)之前没有发送任何标题。当我环绕ifsetcookie,结果是true,但 cookie 没有设置?为什么?

4

1 回答 1

3

您不需要将其添加current time到到期日期吗?

例如

setcookie("y", $unique_string, time() + (60*60*24*30), '/');
于 2013-06-16T08:04:27.503 回答