简体   繁体   中英

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.

Use

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

instead of set_cookie($cookie);

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

    public function cookie()
    {
        $this->load->helper('cookie');

        $name   = 'user';
        $value  = 'pradip';
        $expire = time()+1000;
        $path  = '/';
        $secure = TRUE;

        setcookie($name,$value,$expire,$path); 

        $this->load->view('welcome_message');
    }

Call in view page like echo $this->input->cookie('user');

output = pradip

Say data

$my_cookie= array(

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

   );

Use

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

instead of

set_cookie($my_cookie);

first add this line to the top of your controller

function __construct(){
    parent::__construct();
    $this->load->helper(array('cookie', 'url'));
}

and then set cookie as

set_cookie('counters','ok','999600');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM