简体   繁体   中英

Cookies are there, but i can't get them in CodeIgniter. Why?

Codeigniter: When user logs in I want to save a cookie containing email.

$cookie = array(
    'name'   => 'email',
    'value'  => $email,
    'expire' => time()+3600*24*30*30,
    //'domain' => '.racebooking.net',
    'path'   => '/',
    'secure' => TRUE
);
$this->input->set_cookie($cookie);

Once this code runs, fi look in Firebug i can see the cookie has been properly set. Here is what i see:

name     value                            domain                    raw size    path  expires                Security
email    fontanavideostudios@gmail.com    www.test.racebooking.net  36 B        /     07/09/2062 20:50:15    Secure

Unfortunately, when i try to retrieve it as follows

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

I get nothing at all. The cookie is there, but i can't get it in CI. Any idea why? The website is installed in www.test.racebooking.net (a subdomain i use for testing before going on production)

cookie('some_cookie');

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

Source: https://www.codeigniter.com/user_guide/libraries/input.html

There is also: https://www.codeigniter.com/user_guide/helpers/cookie_helper.html

Ok, (I think) I found the problem! By setting .racebooking.net as domain, instead of leaving it blank (and, hence, automatically setting as www.test.racebooking.net ) i fixed it.

$cookie = array(
    'name'   => 'email',
    'value'  => $email,
    'expire' => time()+3600*24*30*30,
    'domain' => '.racebooking.net',
    'path'   => '/',
    'secure' => TRUE
);
$this->input->set_cookie($cookie);

Now i can retrieve cookie values. The "why does this happen" question remains still open.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM