Jlorenz1
Jlorenz1

Reputation: 91

Codeigniter cookie is set but returns NULL

I just don't get why my cookie return a NULL value. I think I have set the proper syntax in setting a cookie, I have no problem in the if condition I already checked it. Is the global_xss_filtering option in the config file relevant? must it be set to true in order for the cookies to work

I am glad if someone would point out my mistake.

My controller

    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');
        }

Upvotes: 3

Views: 2178

Answers (3)

parth
parth

Reputation: 1868

check the documentation

'secure' => TRUE

is for https only...

also add path and domain as

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

and you have to store the user id in cookies and when page is open... check cookies before checking session...

if user id is set in cookies...get the user and set user in session...

Upvotes: 1

Jlorenz1
Jlorenz1

Reputation: 91

I found the solution to my problem.

I set the expiration to a numeric value and security to false.

Source: https://stackoverflow.com/a/9200289/4779791

Upvotes: 1

Giri Annamalai M
Giri Annamalai M

Reputation: 809

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

        }

Change into:

public function home(){

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

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

        }

You can use it in the view page as well by calling it.

Upvotes: 0

Related Questions