CodeIgniter Forums
cookie won't set - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: cookie won't set (/thread-17665.html)



cookie won't set - El Forum - 04-11-2009

[eluser]SomeFunkyDude[/eluser]
I tried making a controller called "cookie" that sets a cookie in the constructor, then making a simple page2() segment that displays whether the cookie was set or not, but the cookie state keeps returning FALSE

Code:
<?php

class Cookie extends Controller {
    
    function Cookie()
    {
        parent::Controller();
        $this->load->helper('cookie');
        $cookie = array(
                   'name'   => 'cookie_name',
                   'value'  => 'w00t!',
                   'expire' => '1000000',
                   'domain' => '.localhost',
                   'path'   => '/',
                   'prefix' => 'myprefix_',
               );
        set_cookie($cookie);
    }
    
    function page1()
    {
        echo "<p>This is page 1!</p>";
        echo '<a href="page2">Page 2</a>';
    }
    
    function page2()
    {
        echo "<p>This is page 2!</p>";
        if (get_cookie('myprefix_cookie_name') === TRUE)
        {
            echo "<p>Cookie is set</p>";
        } else {
            echo "<p>Cookie not set</p>";
        }
        echo '<a href="page1">Page 1</a>';
    }
}

?&gt;



cookie won't set - El Forum - 04-11-2009

[eluser]bretticus[/eluser]
My advice would be don't set the cookie in your constructor. Cookies are not available on the same page load. calling any method in your controller class runs the set cookie code again. I don't know the ins and outs of the cookie helper functions, but I'd assume that would be your issue. If you have to absolutely want to set the cookie in your constructor, at least use your
Code:
if (get_cookie('myprefix_cookie_name') === TRUE)
code in the constructor to avoid setting your cookie on every page load that references that controller.


cookie won't set - El Forum - 04-11-2009

[eluser]SomeFunkyDude[/eluser]
okay, i revised the cookie to be set only if get_cookie('myprefix_cookie_name') returns false. Apparently
Code:
if (get_cookie('myprefix_cookie_name') === TRUE)
or
Code:
if (get_cookie('myprefix_cookie_name') === FALSE)
wasn't analyzing the Boolean state for some reason.

I tried
Code:
if (get_cookie('myprefix_cookie_name') { execute code }
and it seemed to work.

Code:
&lt;?php

class Cookie extends Controller {
    
    function Cookie()
    {
        parent::Controller();
        $this->load->helper('cookie');
    }
    
    function page1()
    {
        if (!get_cookie('myprefix_cookie_name2'))
        {
            $cookie = array(
                       'name'   => 'cookie_name2',
                       'value'  => 'w00t!2',
                       'expire' => '1000000',
                       'path'   => '/',
                       'prefix' => 'myprefix_',
                   );
            set_cookie($cookie);
        }
        echo get_cookie('myprefix_cookie_name2');
        echo "<p>This is page 1!</p>";
        echo '<a href="page2">Page 2</a>';
    }
    
    function page2()
    {
        echo "<p>This is page 2!</p>";
        if (get_cookie('myprefix_cookie_name2'))
        {
            echo "<p>Cookie is set</p>";
        } else {
            echo "<p>Cookie not set</p>";
        }
        echo '<a href="page1">Page 1</a>';
    }
}

?&gt;



cookie won't set - El Forum - 04-11-2009

[eluser]bretticus[/eluser]
Manual says...

Quote:The function returns FALSE (boolean) if the item you are attempting to retrieve does not exist.

So that makes sense. Glad you got it working. In cases like this you can always get a true statement like:

Code:
if (get_cookie('myprefix_cookie_name') !== FALSE)



cookie won't set - El Forum - 09-05-2012

[eluser]coder_elite[/eluser]
how to make the cookie be available in other controller? for example
Controller 1 - (this is where the cookie is set using set_cookie())
then after the cookie is set in Controller 1 ,redirect to Controller 2.how can i get the cookie (which is set in controller 1) in controller 2.


cookie won't set - El Forum - 09-05-2012

[eluser]CroNiX[/eluser]
http://ellislab.com/codeigniter/user-guide/helpers/cookie_helper.html
http://ellislab.com/codeigniter/user-guide/libraries/input.html