Welcome Guest, Not a member yet? Register   Sign In
Cookies do not work
#1

[eluser]_TTT_[/eluser]
Why cookies do not work ?

in AUTO-LOADER
$autoload['helper'] = array('url','cookie');

I wrote

Code:
function __construct()
{
  parent::Controller();
  set_cookie('pr','10');
}

in index function

Code:
function index()
{
...
  echo get_cookie('pr');
...
}

its empty
#2

[eluser]Dennis Rasmussen[/eluser]
I'm not sure, but I think the page needs to load before the cookie is actually set... as in you can't set and get the same cookie in the same pageload.

Or am I wrong?
#3

[eluser]_TTT_[/eluser]
any other ideas why they do not work?
#4

[eluser]WanWizard[/eluser]
The manual states that that only the name and the value are required.

This seems not to be true. If you use
Code:
set_cookie('pr', '10');
and monitor the requests and reponses, I see the cookie getting set, but with an expiry date 24 hours in the past. Which means that the cookie expires immediately, and that's why you don't see anything.

If you use
Code:
set_cookie('pr', '10', 0);
it is set correctly, and is valid until you close the browser.

If you look into the code, you'll see that 'expire' has an empty string as default, and further in the code, it says
Code:
if ( ! is_numeric($expire))
{
    $expire = time() - 86500;
}

So the manual is wrong at this point. I just checked 2.0 code, where the function is moved to the input class, but the same error exists there.

I suggest you report this in the bug forum, so it can be fixed by Ellislabs.
#5

[eluser]_TTT_[/eluser]
Thanks. One more question. How to safely take the data from cookies in CI

I write like this

$data = $this->my_model->get_records($urisegment,get_cookie('page_per'),$order,$sort_direct);
#6

[eluser]WanWizard[/eluser]
I would use the session for information like that, or store it in the user record in the database if it is a permanent user property.

Cookies are cleaned by the Input library when global xss_clean is enabled. If not, use
Code:
$this->input->cookie('page_per', TRUE)
instead.

And for extra safety you could choose to encrypt the cookie, so it can't be tampered with (which is very easy).




Theme © iAndrew 2016 - Forum software by © MyBB