Cookie example


        $cookieData = $this->front->Request()->getCookie();
        $uniqueId = $this->front->Request()->getCookie('sUniqueID');

        if (!empty($cookieData) && empty($uniqueId)) {
            $basePath = Shopware()->Shop()->getBasePath();
            if ($basePath === null || $basePath === '') {
                $basePath = '/';
            }
            $uniqueId = Random::getAlphanumericString(32);
            $this->front->Response()->headers->setCookie(
                new Cookie(
                    'sUniqueID',
                    $uniqueId,
                    time() + (86400 * 360),
                    $basePath,
                    null,
                    $this->front->Request()->isSecure()
                )
            );
        }

        // Check if this product is already noted


        /** @var Enlight_Controller_Front $controller */
        $controller = $this->container->get('front');

        if ($this->container->initialized('session')) {
            $session = $this->container->get('session');
            $session->offsetSet('auto-user', null);
        }

        $controller->Response()->headers->setCookie(
            new Cookie('slt', null, strtotime('-1 Year')$controller->Request()->getBasePath() . '/')
        );
    }

    public function checkCookie(Enlight_Controller_EventArgs $args)
    {
        $request = $args->getRequest();

        $config = $this->container->get(Shopware_Components_Config::class);

        if (!$config->get('useSltCookie')) {
            return;
        }
if (!empty($row)) {
                    if ($row['cookielifetime']) {
                        $valid = time() + $row['cookielifetime'];
                    } else {
                        $valid = 0;
                    }
                    $basePath = $request->getBasePath();
                    if ($basePath === null || $basePath === '') {
                        $basePath = '/';
                    }
                    $response->headers->setCookie(
                        new Cookie('partner', $row['idcode']$valid$basePath, null, $request->isSecure())
                    );
                }
                Shopware()->Session()->set('sPartner', $partner);
            }
        } elseif ($request->getCookie('partner') !== null) {
            $sql = 'SELECT idcode FROM s_emarketing_partner WHERE active=1 AND idcode=?';
            $partner = Shopware()->Db()->fetchOne($sql[$request->getCookie('partner')]);
            if (empty($partner)) {
                Shopware()->Session()->offsetUnset('sPartner');
            } else {
                Shopware()->Session()->set('sPartner', $partner);
            }
Shopware()->Session()->set('Admin', true);

        $url = $this->Front()->ensureRouter()->assemble(
            [
                'module' => 'frontend',
                'controller' => 'detail',
                'sArticle' => $productId,
            ]
        );

        $this->Response()->headers->setCookie(new Cookie('shop', (string) $shopId, 0, $shop->getBasePath()));
        $this->redirect($url);
    }

    /** * @return Shopware_Components_Translation */
    protected function getTranslationComponent()
    {
        if ($this->translation === null) {
            $this->translation = $this->container->get(Shopware_Components_Translation::class);
        }

        
$shop = $this->getShopRepository()->getActiveById($data['shopId']);
        if ($shop === null) {
            throw new RuntimeException('Shop not found');
        }

        $path = $shop->getBasePath();
        if ($path === null || $path === '') {
            $path = '/';
        }

        // Update right domain cookies         $this->Response()->headers->setCookie(new Cookie('shop', $data['shopId'], 0, $path));
        $this->Response()->headers->setCookie(new Cookie('sUniqueID', Random::getString(20), 0, $path));
        $this->Response()->headers->setCookie(new Cookie('session-' . $data['shopId']$data['sessionId'], 0, $path));

        $this->redirect($shop->getBaseUrl());
    }

    public function unlockCustomerAction()
    {
        $customerId = (int) $this->Request()->getParam('customerId');

        try {
            
$optinService = $this->container->get(\Shopware\Components\OptinServiceInterface::class);

        $data = $optinService->get(OptinServiceInterface::TYPE_THEME_PREVIEW, $hash);

        if (!$data) {
            return false;
        }

        $optinService->delete(OptinServiceInterface::TYPE_THEME_PREVIEW, $hash);

        $this->Response()->headers->setCookie(
            new Cookie(
                $data['sessionName'],
                $data['sessionValue'],
                0,
                $this->Request()->getBaseUrl(),
                null,
                $this->Request()->isSecure()
            )
        );

        // Disable http cache for this Request         $this->Response()->headers->set('cache-control', 'private');

        
// mark request as validated to avoid double validation         $request->setAttribute(self::CSRF_WAS_VALIDATED, true);
    }

    public function clearExistingCookie(): void
    {
        $shop = $this->contextService->getShopContext()->getShop();
        $name = $this->getCsrfName();

        $response = $this->container->get('front')->Response();
        $response->headers->setCookie(new Cookie(
            $name,
            Random::getAlphanumericString(30),
            0,
            sprintf('%s/', $shop->getPath() ?: ''),
            '',
            $shop->getSecure(),
            false
        ));
    }

    /** * Check if the submitted CSRF token matches with the token stored in the cookie or header */
$response->headers->removeCookie($cookieName$currentPath);
                $response->headers->removeCookie($cookieName$currentPathWithoutSlash);
                $response->headers->removeCookie($cookieName$request->getBaseUrl());
                $response->headers->removeCookie($cookieName$cookieBasePath . $request->getBaseUrl());
            }
        }

        foreach ($requestCookies as $cookieKey => $cookieName) {
            if (!$validationFunction($cookieKey)) {
                $host = preg_replace('/^www./', '', $request->getHost());
                foreach ([$host, null] as $cookieHost) {
                    $response->headers->setCookie(new Cookie($cookieKey, null, 0, '/', $cookieHost));
                    $response->headers->setCookie(new Cookie($cookieKey, null, 0, $cookieBasePath$cookieHost));
                    $response->headers->setCookie(new Cookie($cookieKey, null, 0, $cookiePath$cookieHost));
                    $response->headers->setCookie(new Cookie($cookieKey, null, 0, $currentPath$cookieHost));
                    $response->headers->setCookie(new Cookie($cookieKey, null, 0, $currentPathWithoutSlash$cookieHost));
                    $response->headers->setCookie(new Cookie($cookieKey, null, 0, $request->getBaseUrl()$cookieHost));
                    $response->headers->setCookie(new Cookie($cookieKey, null, 0, $cookieBasePath . $request->getBaseUrl()$cookieHost));
                }
            }
        }
    }

    


        $restoreData = $this->paymentTokenService->restore($token);

        // Don't restore, we have already a session         if ($this->session->get('sUserId')) {
            return;
        }

        if ($restoreData) {
            $controller->Response()->headers->setCookie(
                new Cookie(
                    $restoreData->getSessionName(),
                    $restoreData->getValue(),
                    0,
                    \ini_get('session.cookie_path'),
                    null,
                    $request->isSecure()
                )
            );
        }

        $params = $request->getParams();
        

    public function setCookie(
        $name,
        $value = null,
        $expire = 0,
        $path = null,
        $domain = null,
        $secure = false,
        $httpOnly = false
    ) {
        $cookie = new Cookie(
            $name,
            $value,
            $expire,
            $path,
            $domain,
            $secure,
            $httpOnly
        );

        $this->headers->setCookie($cookie);

        
 elseif ($remove) {
            // Remove $noCacheTag from $newCacheTags             $newCacheTags = array_diff($existingTags[$newTag]);
        } elseif (!$remove && !\in_array($newTag$existingTags)) {
            // Add $noCacheTag to $newCacheTags             $newCacheTags = $existingTags;
            $newCacheTags[] = $newTag;
        }

        if (isset($newCacheTags)) {
            $this->response->headers->setCookie(
                new Cookie('nocache', implode(', ', $newCacheTags), 0, $this->request->getBasePath() . '/', null, $this->request->isSecure())
            );
        }
    }

    /** * Helper function to flag the request with cacheIds to invalidate the caching. * * @param array $cacheIds * * @return void */
    

    public function getPluginInformation(Response $response, Request $request)
    {
        if (!$this->isPluginsSubscriptionCookieValid($request)) {
            return false;
        }

        try {
            $cookie = new Cookie('lastCheckSubscriptionDate', date('dmY')time() + 60 * 60 * 24, '/', null, $request->isSecure());
            $response->headers->setCookie($cookie);

            return $this->getPluginInformationFromApi();
        } catch (ShopSecretException $e) {
            $this->exception = $e;
            $this->resetShopSecret();

            return false;
        } catch (Exception $e) {
            $this->exception = $e;

            
$removePath = $request->getBasePath() . '/shopware.php';
            $newPath = str_replace($removePath$request->getBasePath()$request->getRequestUri());
            $newPath = preg_replace('/\/{2,}/', '/', $newPath);
        }

        if (isset($newPath)) {
            // reset the cookie so only one valid cookie will be set IE11 fix             $basePath = $shop->getBasePath();
            if ($basePath === null || $basePath === '') {
                $basePath = '/';
            }
            $response->headers->setCookie(new Cookie('session-' . $shop->getId(), '', 1, $basePath));
            $response->setRedirect($newPath, 301);
        } else {
            $this->upgradeShop($request$response);
            $this->initServiceMode($request);
        }

        $this->get(ContextServiceInterface::class)->initializeShopContext();
    }

    public function getCapabilities()
    {
        
Home | Imprint | This part of the site doesn't use cookies.