Skip to content

Commit

Permalink
Merge pull request #27 from 126016/fix-max-age-cookie-value
Browse files Browse the repository at this point in the history
Fix the value of the max age for cookie to 30days
  • Loading branch information
urbantrout authored Jan 26, 2022
2 parents dab4191 + 96eeca5 commit 5fde474
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Classes/Middleware/RedirectionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

/**
* Returns redirect response based on users browser language.
* Sets the cookie for 30 days.
*
* @param ServerRequestInterface $request
* @param string $cookieName
Expand Down Expand Up @@ -191,13 +192,14 @@ function ($language) use ($siteLanguages, $siteLanguagesFallbacks) {
);

/** @var RedirectResponse $response */

$response = new RedirectResponse($uri, 307);
return $response->withAddedHeader('Set-Cookie', $cookieName . '=' . $matchingSiteLanguage->getLanguageId() . '; Path=/; Max-Age=' . (time()+60*60*24*30));
return $response->withAddedHeader('Set-Cookie', $cookieName . '=' . $matchingSiteLanguage->getLanguageId() . '; Path=/; Max-Age=' . (60*60*24*30));
}

/**
* Returns redirect response based on users IP address. GeoIP2 is used to
* get the country based on the visitors IP address.
* get the country based on the visitors IP address. Sets the cookie for 30 days.
*
* @param ServerRequestInterface $request
* @param string $cookieName
Expand Down Expand Up @@ -271,15 +273,15 @@ function ($siteLanguage) use ($geocodedIsoCode) {

/** @var RedirectResponse $response */
$response = new RedirectResponse($uri, 307);
return $response->withAddedHeader('Set-Cookie', $cookieName . '=' . $matchingSiteLanguage->getLanguageId() . '; Path=/; Max-Age=' . (time()+60*60*24*30));
return $response->withAddedHeader('Set-Cookie', $cookieName . '=' . $matchingSiteLanguage->getLanguageId() . '; Path=/; Max-Age=' . (60*60*24*30));
} catch (\Throwable $e) {
// IP address is not in database. Do not redirect.
return null;
}
}

/**
* Sets cookie with preferred language when user changes language in the frontend.
* Sets cookie with preferred language when user changes language in the frontend for 30 days.
*
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
Expand Down Expand Up @@ -317,7 +319,7 @@ protected function setCookieOnLanguageChange(ServerRequestInterface $request, Re
strpos($refererUri->getPath(), $requestLanguage->getBase()->getPath()) !== 0 ||
($requestLanguage->getBase()->getPath() === '/' && !in_array($requestLanguage->getBase()->getPath(), $siteLanguageBasePaths))
) {
$response = $response->withAddedHeader('Set-Cookie', $cookieName . '=' . $requestLanguage->getLanguageId() . '; Path=/; Max-Age=' . (time()+60*60*24*30));
$response = $response->withAddedHeader('Set-Cookie', $cookieName . '=' . $requestLanguage->getLanguageId() . '; Path=/; Max-Age=' . (60*60*24*30));
}
return $response;
}
Expand Down

0 comments on commit 5fde474

Please sign in to comment.