13

I can print the session values in codeigniter by print_r($this->session->userdata); How can I print the cookies in codeigniter? I have just set a cookie:

$cookie = array(
          'name'   => 'test_cookie',
          'value'  => 'test',
          'domain' => '/',
          'secure' => TRUE
          );

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

How can i print the above cookie?

Anthony Geoghegan
  • 11,533
  • 5
  • 49
  • 56
Roman
  • 3,764
  • 21
  • 52
  • 71

7 Answers7

20

Look at the documentation: Codeigniter Cookie Helper Guide

It says that you should use $this->input->cookie() to retrieve a cookie:

$this->input->cookie('test_cookie', TRUE);
tmarois
  • 2,424
  • 2
  • 31
  • 43
Repox
  • 15,015
  • 8
  • 54
  • 79
  • i try to print this, but it doesnt print anything, neither gives any error. – Roman Jun 20 '11 at 11:38
  • And you are absolutely sure that the cookie is set? Try using var_dump($this->input->cookie('test_cookie')); and see if this return false - if so, the cookie isn't set. – Repox Jun 20 '11 at 11:39
  • i get `bool(false) `. I'm trying to set the cookie as i mentioned in my question. and i'm loading the cookie helper before setting cookie `$this->load->helper('cookie');`. Any idea whats wrong? – Roman Jun 20 '11 at 11:46
  • @okay, i figured out the problem. I was passing time parameter as a string. – Roman Jun 20 '11 at 11:58
  • Try setting your cookie like this, for testing purposes: $this->input->set_cookie('test_cookie', 'My cookie value'); I believe your problem lies with the domain value, as your domain can't be / but has to be a valid domain. – Repox Jun 20 '11 at 11:58
6

This worked for me on localhost, security might need tightened for server

$this->load->helper('cookie');     
$cookie = array(
                    'name'   => 'data',
                    'value'  => '23',
                    'expire' =>  86500,
                    'secure' => false
                );
                $this->input->set_cookie($cookie); 
                var_dump($this->input->cookie('data', false));  

Expire needs to be numeric, removed path and set secure to false

Michael L Watson
  • 964
  • 13
  • 23
3

If you are using google chrome use inspect element to see if the cookie has been set... I think you can do it in FF, but I haven't used FF in a while... I only had one issue with cookies and that was I was setting domain to my live domain... So I have my cookie code like this:

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

         $cookie = array(
           'name'   => 'the_cookie',
           'value'  => 'test value here',
           'expire' => '15000000',
           'prefix' => ''
        );
        $this->input->set_cookie($cookie);

Here you can see it is showing up in Google Chrome "Inspect Element Tool"

Google chrome displaying the_cookie value

jason
  • 1,132
  • 14
  • 32
  • after surfing 10 pages came to this and worked. using domain as element of cookie array and failed to create cookie. This helped finally thanx. – TechCare99 Sep 06 '13 at 16:15
1
'secure' => TRUE

This does not allow me to fetch the cookie.


just set

'secure' => FALSE 

and see it may work.

Raj
  • 706
  • 8
  • 18
1

setting the security => TRUE will not allow to print the cookie value in local, it only grant access to secure connections only so it will not print anything in localhost for you unless you set the security => FALSE than using codeigniter CI_Input class you can get the value of cookie

$this->input->cookie('cookie_name', TRUE);  //with xss filtering 
$this->input->cookie('cookie_name');        //without xss filtering
0

If the code mentioned below does not provide any output, then modify the application/config/config.php file and setting this:

$config['global_xss_filtering'] = TRUE;

$this->input->cookie('cookie_name', TRUE);

else just use this it will display the value

$this->input->cookie('cookie_name'); 
Ganesh RJ
  • 942
  • 2
  • 19
  • 31
Vinit Kadkol
  • 1,221
  • 13
  • 12
0

Load the cookie helper with:

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

Then retrieve your cooking with:

$cookieData = get_cookie("cookie_name");

Note, these are aliases to using the input class, you can also retrieve and set cookies like so:

$cookieData = $this->input->get_cookie("cookie_name");

Source http://ellislab.com/codeigniter/user-guide/helpers/cookie_helper.html

ritesh
  • 2,245
  • 2
  • 25
  • 37
Eddie
  • 12,898
  • 3
  • 25
  • 32