Skip to content
Snippets Groups Projects
Verified Commit 052330a3 authored by Thomas Flori's avatar Thomas Flori
Browse files

add options to set cookie method

parent aa3d2026
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment