Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set cookie in codeigniter

i tried following code for set cookie but i can't get the cookie.

if($this->input->post('remember')){                    
                $this->load->helper('cookie');
                $cookie = array(
                        'name'   => 'remember_me',
                        'value'  => 'test',                            
                        'expire' => '300',                                                                                   
                        'secure' => TRUE
                        );
               set_cookie($cookie);                   
   }

and following code is for get cookie

$cookie= get_cookie('remember_me');  
 var_dump($cookie);

can any one tell me what's the problem is there? thanks in advance.

like image 470
Keyur Chavda-kc1994 Avatar asked Nov 24 '25 16:11

Keyur Chavda-kc1994


2 Answers

Use

$this->input->set_cookie($cookie);

instead of set_cookie($cookie);

like image 182
momouu Avatar answered Nov 27 '25 04:11

momouu


You need to create a controller class and add the following code to it;

<?php

if ( ! defined('BASEPATH')) exit('Stop Its demostrate how to set cookie');

class cw_cookies extends CI_Controller {

   function __construct()

   {

       parent::__construct();

       $this->load->helper('cookie');

   }



   function set()

   {

       $cookie= array(

           'name'   => 'remember_me',
           'value'  => 'test',                            
           'expire' => '300',                                                                                   
           'secure' => TRUE

       );

       $this->input->set_cookie($cookie);

       echo "Congratulation Cookie Set";

   }



   function get()

   {

       echo $this->input->cookie('remember_me',true);

   }

}

The above code sets the cookies through

$this->input->set_cookie()

The helper is loaded using:

$this->load->helper('cookie');

You can read more at : Set Cookies in Codeigniter

like image 37
Shahroze Nawaz Avatar answered Nov 27 '25 04:11

Shahroze Nawaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!