3

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');
        }
Jlorenz1
  • 91
  • 9
  • 1
    you cant check a cookie untll the next page load –  Aug 26 '15 at 04:16
  • Thank you for the answer sir, I just want to clarify what if I successfully login and stored a cookie for a "remember me" functionality. After I logged in, Ill close the browser, will the cookie already have the value set the time i open my website again in a newly opened browser? – Jlorenz1 Aug 26 '15 at 04:21
  • Hi sir, still returns null after redirecting it to the controller that manages the view – Jlorenz1 Aug 26 '15 at 04:45

3 Answers3

1

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...

Sparky
  • 98,165
  • 25
  • 199
  • 285
parth
  • 1,803
  • 2
  • 19
  • 27
  • I am using a localhost so I changed the secure variable to FALSE, unfortunately the cookie value stays NULL – Jlorenz1 Aug 26 '15 at 04:52
  • have you add $this->load->helper('cookie');...?? and clean your cookies in browser.. – parth Aug 26 '15 at 04:54
  • I have set it in the config file together with form and url so I assume it already runs...I will try cleaning cookies on my browser – Jlorenz1 Aug 26 '15 at 04:55
  • please add path and domain as above – parth Aug 26 '15 at 04:58
  • it still has a NULL value, I appreciate your help – Jlorenz1 Aug 26 '15 at 05:20
  • I only added the domain as said in the documentation the path can be default on root. – Jlorenz1 Aug 26 '15 at 06:18
  • if u are running your application in local and subfolder of xampp or wamp like 'http://localhost/app/'...than u hve to specify d '/app' in path...'/' mean your app path is 'http://localhost/' – parth Aug 26 '15 at 10:22
1

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

Community
  • 1
  • 1
Jlorenz1
  • 91
  • 9
  • Thank you, yep I rushed into conclusion possibly because of joy at last that my cookie has returned my value. I tested putting back the ' ' and it still worked – Jlorenz1 Aug 26 '15 at 16:31
0
//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.

Giri Annamalai M
  • 810
  • 9
  • 24
  • Still has the null value of the 'remember' cookie – Jlorenz1 Aug 26 '15 at 05:46
  • $cookie_email = array( 'name' => 'admin_email', 'value' => $this->encrypt->encode($this->input->post('email')), 'expire' => 20*365)); secure only supports https pages. Instead of you can use encrypt library – Giri Annamalai M Aug 26 '15 at 06:09
  • noted sir , though my concern is why can't I get a value from a cookie I set at the first controller. Does the .htaccess file affect this. – Jlorenz1 Aug 26 '15 at 06:16
  • Nope. It won't. you can directly call it in you view page as print_r(get_cookie('cookie')); try this.... – Giri Annamalai M Aug 26 '15 at 06:22