douke1905
2017-02-10 12:21
浏览 75
已采纳

如何在cakephp中1分钟后破坏cookie?

I am new in cakephp 3.0. i have successfull created a cookie but i want to destroy that cookie after one minute. i have done so far:-

public function register_cookie(){
    $data = "Hello world!";
    $this->Cookie->write('dataFetch', $data, true, time() + (60 * 1));
}
public function getcookie() {
        $cookiedata = $this->Cookie->read('dataFetch');
        echo $cookiedata;
}

but when i come after one minute in getCookie function it still prints i.e. "Hello World" i want after one minute cookie is expired. Thanks in advance :)

图片转代码服务由CSDN问答提供 功能建议

我是cakephp 3.0中的新手。 我已成功创建了一个cookie,但我想在一分钟后销毁该cookie。 到目前为止我做了: -

  public function register_cookie(){
 $ data =“Hello world!”; 
 $ this-> Cookie-> write  ('dataFetch',$ data,true,time()+(60 * 1)); 
} 
公共函数getcookie(){
 $ cookiedata = $ this-> Cookie-> read('dataFetch'  ); 
 echo $ cookiedata; 
} 
   
 
 

但是当我在 getCookie 函数中一分钟后来时,它仍会打印出来,即“ Hello World“ i想要在一分钟后cookie过期。 谢谢提前:)

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doutu7123 2017-02-10 12:58
    已采纳

    In cakephp 3.x as the document says you can have these paramenters

    CookieComponent::write(mixed $key, mixed $value = null)

    But in cakephp 2.x it use to take these parameters

    CookieComponent::write(mixed $key, mixed $value = null, boolean $encrypt = true, mixed $expires = null)

    To set expiry time you have to set the configuration like this

    $this->Cookie->config([
        'expires' => '+10 days',
    ]);
    

    So you code will be like this

    public function register_cookie(){
        $this->Cookie->config([
            'expires' => '+1 minute',
        ]);
        $this->Cookie->configKey('dataFetch', 'encryption', false);
        $data = "Hello world!";
        $this->Cookie->write('dataFetch', $data);
    }
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题