Keyur Chavda-kc1994
Keyur Chavda-kc1994

Reputation: 1045

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.

Upvotes: 3

Views: 59977

Answers (5)

LIsa
LIsa

Reputation: 65

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

Upvotes: 1

Sani Kamal
Sani Kamal

Reputation: 1238

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

Upvotes: 1

Pradip Parmar
Pradip Parmar

Reputation: 301

    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

Upvotes: 1

Shahroze Nawaz
Shahroze Nawaz

Reputation: 589

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

Upvotes: 4

momouu
momouu

Reputation: 711

Use

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

instead of set_cookie($cookie);

Upvotes: 7

Related Questions