Added ability to remove cookies.

This commit is contained in:
flash 2023-01-05 02:33:24 +00:00
parent b67f5c8e90
commit 884d2cb9da

View file

@ -61,7 +61,10 @@ class HttpResponseBuilder extends HttpMessageBuilder {
$expires = DateTime::createFromInterface($expires); $expires = DateTime::createFromInterface($expires);
} }
$maxAge = $expires->difference(DateTime::now())->totalSeconds(); $now = DateTime::now();
$maxAge = $expires->isLessThanOrEqual($now)
? -1 : $expires->difference($now)->totalSeconds();
$expires = $expires->toCookieString(); $expires = $expires->toCookieString();
$cookie .= '; Expires=' . $expires . '; Max-Age=' . $maxAge; $cookie .= '; Expires=' . $expires . '; Max-Age=' . $maxAge;
} }
@ -81,6 +84,17 @@ class HttpResponseBuilder extends HttpMessageBuilder {
$this->addHeader('Set-Cookie', $cookie); $this->addHeader('Set-Cookie', $cookie);
} }
public function removeCookie(
string $name,
string $path = '',
string $domain = '',
bool $secure = false,
bool $httpOnly = false,
bool $sameSiteStrict = false
): void {
$this->addCookie($name, '', -9001, $path, $domain, $secure, $httpOnly, $sameSiteStrict);
}
public function redirect(string $to, bool $permanent = false): void { public function redirect(string $to, bool $permanent = false): void {
$this->setStatusCode($permanent ? 301 : 302); $this->setStatusCode($permanent ? 301 : 302);
$this->setHeader('Location', $to); $this->setHeader('Location', $to);