From 052330a3db1b5538b1563b7eabd23206da83506b Mon Sep 17 00:00:00 2001 From: Thomas Flori <thflori@gmail.com> Date: Mon, 6 Aug 2018 07:16:26 +0200 Subject: [PATCH] add options to set cookie method --- src/ServerResponse.php | 43 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/src/ServerResponse.php b/src/ServerResponse.php index f7792aa..2316037 100644 --- a/src/ServerResponse.php +++ b/src/ServerResponse.php @@ -2,7 +2,6 @@ namespace Tal; -use Psr\Http\Message\StreamInterface; use Tal\Psr7Extended\ServerResponseInterface; class ServerResponse extends Response implements ServerResponseInterface @@ -53,25 +52,53 @@ class ServerResponse extends Response implements ServerResponseInterface public function setCookie( $name, $value = "", - $expire = 0, + $maxAge = 0, $path = "", $domain = "", $secure = false, - $httponly = false + $httponly = false, + $sameSite = false ) { + if (preg_match('/[=,; \t\r\n\013\014]/', $name)) { + throw new \InvalidArgumentException( + 'Cookie names cannot contain any of the following \'=,; \t\r\n\013\014\'' + ); + } + $headerLine = sprintf('%s=%s', $name, urlencode($value)); - if ($expire) { - $headerLine .= '; expires=' . gmdate('D, d M Y H:i:s T', time() + $expire); - $headerLine .= '; max-age=' . $expire; + + if ($maxAge) { + $headerLine .= '; expires=' . gmdate('D, d M Y H:i:s T', time() + $maxAge); + $headerLine .= '; Max-Age=' . $maxAge; + } + + if ($path) { + $headerLine .= '; path=' . $path; + } + + if ($domain) { + $headerLine .= '; domain=' . $domain; } - // @todo prepare the header with all options given + + if ($secure) { + $headerLine .= '; secure'; + } + + if ($httponly) { + $headerLine .= '; HttpOnly'; + } + + if ($sameSite) { + $headerLine .= '; SameSite'; + } + $this->addHeader('Set-Cookie', $headerLine); return $this; } public function deleteCookie($name) { - $this->setCookie($name, 'deleted', -1); + $this->setCookie($name, 'deleted', 1); return $this; } } -- GitLab