0

我正在使用codeigniter,并且在我的库文件中存储了cookie,该cookie在打开浏览器时可以正常工作,但是在我关闭浏览器时会过期。代码中有什么问题吗?

        $this->CI =& get_instance();
        $this->CI->load->helper('cookie');
        $expire = time()+(60*60*24*30);
        $cookie = array(
                       'name'   => 'username',
                       'value'  => $users['username'],
                       'expire' => $expire
                   );
        $this->CI->input->set_cookie($cookie);
        $cookie = array(
                       'name'   => 'password',
                       'value'  => $users['password'],
                       'expire' => $expire
                   );
        $this->CI->input->set_cookie($cookie);
4

1 回答 1

4

The problem is the expiration time you are sending ... this is from the help page here

The expiration is set in seconds, which will be added to the current time. Do not include the time, but rather only the number of seconds from now that you wish the cookie to be valid. If the expiration is set to zero the cookie will only last as long as the browser is open.

So change your code to this :

$expire = (60*60*24*30);

To set the cookie to expire 30 days from now

(Although setting it like you did should probably have worked - but the expiration would be years in advance ... the help document also shows the expiration given as a string - maybe thats the problem)

于 2012-05-17T12:00:09.010 回答