简体   繁体   中英

how to retrieve codeigniter's cookie using javascript?

In my codeigniter's controller I have the following code:

$cookie = array(
          'name'   => 'my_name',
          'value'  => 'my value goes here'
          );

$this->input->set_cookie($cookie); 

However, when I tried to retrieve the cookie using javascript's document.cookie, it printed strings like cookie_name, csrf_cookie_name but not my_name and 'my value goes here'. Why?

Note: if I use the php function setcookie('my_name', 'my value goes here') then it works fine, it just I can't make it work with codeigniter's cookie helper.

figured out.

According to the official documentation, the required fields when setting the cookie are name and value, but actually if you don't specify the expire value, it will never work.

So I changed the $cookie value to:

$cookie = array(
          'name'   => 'my_name',
          'value'  => 'my value goes here',
          'expire' => '86500'
          );

and now it's perfectly fine :)

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