No se puede configurar la cookie dentro de la construcción en codeigniter

I trying to set a cookie if null but I can't get it to work:

public function __construct()
    if ($this->input->cookie('ff', TRUE) == FALSE)
        $this->input->set_cookie('ff', 'on', 86500);
        dump($this->input->cookie('ff', TRUE));

¿Qué estoy haciendo mal?

Edit: dump() it's just a custom debug function.

preguntado el 30 de enero de 12 a las 19:01

¿Qué dump() do? Cookies need to be set before anything else is sent to the browser as they are in the response headers. -

dump is a custom debug function. Then how can I properly set in codeigniter a cookie if empty? -

You can use a number of ways to debug without affecting the ability to set headers. You can use Firebug with FirePHP, email yourself the output with mail() or write to a log file or use PHPs output buffering. Additionally in this case you could just put your call to dump() after you have set the cookie. -

los dump() function isn't the problem, I've tried all sorts of things... the cookie just won't set. It should give no problems when setting a cookie in construct, right? -

1 Respuestas

A couple of reasons your set_cookie call might fail:

1) You've output something to the browser already when set_cookie is called, in which case you might get an error/notice along the lines of "Headers already sent". To fix that, just ensure that the set_cookie call occurs before sending anything to the browser.

2) If you're doing local development and testing, "localhost" is not considered a valid domain, so no cookies will be saved for it. A workaround would be to add an entry in your hosts file, for example:

And then test with that domain, instead of "localhost".

Respondido el 31 de enero de 12 a las 03:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.