簡體   English   中英

設置了Codeigniter Cookie,但返回NULL

[英]Codeigniter cookie is set but returns NULL

我只是不明白為什么我的cookie返回一個NULL值。 我想我在設置cookie時設置了正確的語法,如果我已經檢查過它的if條件,我沒有問題。 配置文件中的global_xss_filtering選項是否相關? 必須將其設置為true才能使cookie工作

我很高興有人指出我的錯誤。

我的控制器

    public function store_cookie(){
            if($this->input->post('remember_me') == 'remember'){
                                $cookie = array(
                                'name'   => 'remember',
                                'value'  => 'logged_in',
                                'expire' => '86500',
                                'secure' => TRUE
                                );
                            $this->input->set_cookie($cookie);
                            var_dump($this->input->cookie($cookie));
                            //This is where i do my checking and it returns NULL
                            die(); 
                            //model
                            $this->load->model('admin_model');
                            $this->admin_model->store_cookie($this->input->post('email'),$this->input->post('remember_me'));

                            }

                }


public function validate_credentials2(){
        $this->load->model('admin_model');
        $query = $this->admin_model->validate();

        if($query){
                // if($this->input->post('remember_me')== 'remember'){
                //  $cookie = array(
                //         'name'   => 'remember',
                //         'value'  => 'logged_in',
                //         'expire' => '86500',
                //         'secure' => TRUE
                //  );
                // $this->input->set_cookie($cookie);
                // }
                $this->store_cookie();

                $data = array(
                    'email' => $this->input->post('email'),
                    'is_logged_in' => TRUE,
                    'role' => 'admin'
                    );
                $this->session->set_userdata('counter2', 0);
                $this->session->set_userdata($data);
                redirect('admin/home', 'refresh');
}

//admin controller
public function home(){
             var_dump($this->input->cookie('remember')); //still returns NULL
             die(); 
            $this->load->view('ui/admin_home');
        }

檢查文檔

'secure' => TRUE

僅用於https ...

還添加路徑和域為

'domain' => '.localhost',
'path'   => '/path_to_the_folder_name',

並且您必須將用戶ID存儲在cookie中以及頁面打開時...在檢查會話之前先檢查cookie ...

如果在cookie中設置了用戶ID ...獲取用戶並在會話中設置用戶...

//admin controller
public function home(){
         var_dump($this->input->cookie('remember')); //still returns NULL
             die(); 
          $this->load->view('ui/admin_home');

        }

變成:

public function home(){

         $data['cookie'] = $this->input->cookie('rememeber', TRUE);

          $this->load->view('ui/admin_home',$data);

        }

您也可以通過在視圖頁面中調用它來使用它。

我找到了解決我問題的方法。

我將到期時間設置為數值,並將安全性設置為false。

資料來源: https : //stackoverflow.com/a/9200289/4779791

暫無
暫無

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

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