Read and Write Cookie in Symfony2

前端 未结 6 535
無奈伤痛
無奈伤痛 2020-12-12 18:24

I want to store some information in the local browser cookie. After hours looking for a nice tutorial, I managed to store some data in a non-session cookie:

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-12 18:52

                use Symfony\Component\HttpFoundation\Cookie;
                use Symfony\Component\HttpFoundation\Response;
    
                // set current active tab in cookie 
                $cookie = new Cookie('myProfileActiveTab', 'myaddress', strtotime('now + 60 minutes'));
                $response = new Response();
                $response->headers->setCookie($cookie);
                $response->send();
    
    
               // get current active tab from cookies
               $cookies = $request->cookies;
               if ($cookies->has('myProfileActiveTab')) {
                   $activetab = $cookies->get('myProfileActiveTab');
               } 
    

提交回复
热议问题