doudao7113 2015-04-14 11:24
浏览 189
已采纳

Symfony将数据存储在缓存问题中

I'm struggling with this, I need to store data in the browser's cache. I'm using Symfony 2.6.1, I use the following code:

    if($request->cookies->get('myCookie')){
        /*  do stuff  */

    }else{
        $cookie = new Cookie('myCookie', $myData, 0, '/', null, false, false);            
        $response->headers->setCookie($cookie);           
        $response->setCache(array(
            'etag'          => 'abcdef',
            'last_modified' => new \DateTime(),
            'max_age'       => 600,
            's_maxage'      => 600,
            // 'private'       => false,
            'public'        => true,
        ));
        $response->send(); 

        /* send a response */
    }

I need to 1st check if the cookie "myCookie" exists, if not it should create and store in the browser. My problem is that, when I simply refresh the page to cookie is there, but if I close the browser, when I open again the cookie is not there.

I think that is some configuration/ propriety in the in the array when I set the cookie $response->setCache(array( ... ))

In the official site, there is not many helpful info about this.

http://symfony.com/doc/current/book/http_cache.html#types-of-caches

  • 写回答

1条回答 默认 最新

  • duan5801 2015-04-14 11:46
    关注

    Cookie being gone actually makes sense. You're constructing the cookie with line:

    $cookie = new Cookie('myCookie', $myData, 0, '/', null, false, false); 
    

    The third argument (0) means that it's a session cookie - will be discarded as soon as you close your browser.

    Try something like this:

    // Cookie stays alive for an hour
    $cookie = new Cookie('myCookie', $myData, time() + 3600, '/', null, false, false); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度