setcookie()没有错误,但cookie没有设置

时间:2013-06-16 07:50:32

标签: php codeigniter cookies

我有一个基于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之前没有发送任何标题(对于登录页面)。当我在if周围包裹setcookie时,结果是true,但cookie未设置?为什么呢?

1 个答案:

答案 0 :(得分:3)

您是否需要将current time添加到过期日期?

例如

setcookie("y", $unique_string, time() + (60*60*24*30), '/');