Skip to content

Commit aae0611

Browse files
Merge pull request #1 from ojhaujjwal/constructor-domain
constructor domain
2 parents 830445e + 4ee3986 commit aae0611

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/Cookie.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,15 @@ final class Cookie
8787
*
8888
* @param string $name The name of the cookie which is also the key for
8989
* future accesses via `$_COOKIE[...]`.
90+
* @param string $domain The domain that the cookie will be valid for (including all subdomains)
9091
*/
91-
public function __construct(string $name)
92+
public function __construct(string $name, string $domain = null)
9293
{
9394
$this->name = $name;
9495
$this->value = null;
9596
$this->expiryTime = 0;
9697
$this->path = '/';
97-
$this->domain = self::normalizeDomain($_SERVER['HTTP_HOST']);
98+
$this->setDomain(self::normalizeDomain($domain ?? $_SERVER['HTTP_HOST']));
9899
$this->httpOnly = true;
99100
$this->secureOnly = false;
100101
$this->sameSiteRestriction = self::SAME_SITE_RESTRICTION_STRICT;

tests/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
testEqual((new \ParagonIE\Cookie\Cookie('key'))->setValue('value')->setDomain('.localhost'), 'Set-Cookie: key=value; path=/; httponly; SameSite=Lax');
107107
testEqual((new \ParagonIE\Cookie\Cookie('key'))->setValue('value')->setDomain('127.0.0.1'), 'Set-Cookie: key=value; path=/; httponly; SameSite=Lax');
108108
testEqual((new \ParagonIE\Cookie\Cookie('key'))->setValue('value')->setDomain('.local'), 'Set-Cookie: key=value; path=/; httponly; SameSite=Lax');
109-
testEqual((new \ParagonIE\Cookie\Cookie('key'))->setValue('value')->setDomain('example.com'), 'Set-Cookie: key=value; path=/; domain=.example.com; httponly; SameSite=Lax');
109+
testEqual((new \ParagonIE\Cookie\Cookie('key', 'example.com'))->setValue('value'), 'Set-Cookie: key=value; path=/; domain=.example.com; httponly; SameSite=Lax');
110110
testEqual((new \ParagonIE\Cookie\Cookie('key'))->setValue('value')->setDomain('.example.com'), 'Set-Cookie: key=value; path=/; domain=.example.com; httponly; SameSite=Lax');
111111
testEqual((new \ParagonIE\Cookie\Cookie('key'))->setValue('value')->setDomain('www.example.com'), 'Set-Cookie: key=value; path=/; domain=.example.com; httponly; SameSite=Lax');
112112
testEqual((new \ParagonIE\Cookie\Cookie('key'))->setValue('value')->setDomain('.www.example.com'), 'Set-Cookie: key=value; path=/; domain=.example.com; httponly; SameSite=Lax');

0 commit comments

Comments
 (0)