public function onSendResponse(SendResponseEvent $event)
 {
     if (!$this->modified || !$event->isMasterRequest()) {
         return;
     }
     $cookie = new SetCookieHeader(self::COOKIE_NAME);
     $cookie->setDiscard(true);
     $cookie->setHttpOnly(true);
     $cookie->setPath('/' . ltrim($event->getRequest()->getBaseUri()->getPath(), '/'));
     if (empty($this->messages)) {
         $cookie->setExpires(new \DateTime('@1337'));
     } else {
         $data = json_encode($this->messages, JSON_UNESCAPED_SLASHES);
         if ($this->signature !== NULL) {
             $data = $this->signature->sign($data);
         }
         $cookie->setValue(base64_encode($data));
     }
     $event->getResponse()->setCookie($cookie);
 }
 public function process(DispatchRequest $request)
 {
     if (!$request->isMaster() || $this->session->isInitialized()) {
         return $request->proceed();
     }
     $httpRequest = $request->getHttpRequest();
     $name = (string) $this->config->getString('session.name', 'sid');
     if ($httpRequest->hasCookie($name)) {
         $this->session->initialize($httpRequest->getCookie($name));
     } else {
         $this->session->initialize();
     }
     $response = $request->proceed();
     if ($this->session->isStarted()) {
         $response->setHeader('Cache-Control', 'no-cache,no-store,max-age=0,must-revalidate,proxy-revalidate');
         $response->setHeader('Pragma', 'no-cache');
         $response->setHeader('P3P', 'CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"');
     }
     if ($this->session->isNew()) {
         $config = $this->config->getConfig('cookie');
         if ($config->has('path')) {
             $path = $config->getString('path');
         } else {
             $path = '/' . ltrim($request->getHttpRequest()->getBaseUri()->getPath() . '/', '/');
         }
         $cookie = new SetCookieHeader($name, $this->session->getIdentifier());
         $cookie->setDiscard($config->getBoolean('discard', true));
         $cookie->setHttpOnly($config->getBoolean('httpOnly', true));
         $cookie->setPath($path);
         if ($config->has('domain')) {
             $cookie->setDomain($config->getString('domain'));
         }
         $response->setCookie($cookie);
     }
     $this->session->close();
     return $response;
 }
Example #3
0
 /**
  * Remove a cookie by eliminating a Set-Cookie header for this cookie replacing it with a
  * header that will cause the client to remove the cookie.
  * 
  * @param string $name Name of the cookie to be removed.
  * @return HttpResponse
  */
 public function removeCookie($name)
 {
     $cookie = new SetCookieHeader($name, '');
     $cookie->setExpires(1337);
     $this->addHeader($cookie);
     return $this;
 }