CodeIgniter Forums

Full Version: Basic Cookie Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Jim Higgins[/eluser]
I'm having a bit of trouble accessing a cookie using the Cookie Helper. After loading the helper of course, should this work?

$this->_state = 'Some State Name';
set_cookie('state_cookie', $this->_state);
echo 'Get cookie = ' . get_cookie('state');

The get cookie returns nothing. I'm not receiving any errors. I stripped everything down to the basics here.

El Forum

[eluser]Jay Turley[/eluser]
[quote author="Jim Higgins" date="1207106975"]I'm having a bit of trouble accessing a cookie using the Cookie Helper. After loading the helper of course, should this work?

$this->_state = 'Some State Name';
set_cookie('state_cookie', $this->_state);
echo 'Get cookie = ' . get_cookie('state');

The get cookie returns nothing. I'm not receiving any errors. I stripped everything down to the basics here.[/quote]

Just guessing, really, since I have yet to use the CI cookie functions, but shouldn't you use the same name for the retrieval as for the setting?

Code:
$this->_state = 'Some State Name';
set_cookie('state_cookie', $this->_state);
echo 'Get cookie = ' . get_cookie('state_cookie');
----------------------------------------^^^^^^^

El Forum

[eluser]Jim Higgins[/eluser]
Yeah. I am. Sorry. I made a mistake when simplifying for the post. Here's what it actually looks like...

if ($this->input->post('state')) {
$this->_state = $this->input->post('state');
set_cookie('state_cookie', $this->_state);
echo 'Get cookie = ' . get_cookie('state_cookie');
}

El Forum

[eluser]Jay Turley[/eluser]
Okay, first of all, naming the cookie state_cookie caused me to get the mysterious "Disallowed Key Characters." error message, indicating that name conflicts with another cookie I already have in IE.

Second, I found the following code to work perfectly:

Code:
set_cookie('squig', 'mybutt', '0');

    echo 'Cookie =' . get_cookie('squig');

However when I deleted the third "non-required" parameter, the expiration, the cookie did not work. Try adding the '0' (or however many seconds you want) as the third parameter to set_cookie.

El Forum

[eluser]Jim Higgins[/eluser]
Hey, you're right! I tried adding the 0 and it worked just fine. Thanks.