Skip to content

Commit dfc4517

Browse files
Merge pull request #12 from Crowdhandler/fix/set-cookie-domain
Added methods to get and set the cookie domain
2 parents a661f25 + 5223b88 commit dfc4517

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

src/GateKeeper.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class GateKeeper
2424
private $safetyNetSlug;
2525
private $debug = false;
2626
private $timer;
27+
private $cookieDomain;
2728
private $ignoreRequest = false;
2829
public $token;
2930
public $ip='192.168.0.1';
@@ -51,6 +52,8 @@ public function __construct(Client $client, \Psr\Http\Message\ServerRequestInter
5152
$cookies = $_COOKIE;
5253
}
5354

55+
$this->setCookieDomain($server);
56+
5457
if (isset($server['REQUEST_SCHEME'])){
5558
$this->ignoreRequest = strtolower($server['REQUEST_SCHEME']) != "https";
5659
}
@@ -247,13 +250,34 @@ public function getRedirectUrl()
247250
return $this->redirectUrl;
248251
}
249252

253+
/**
254+
* Set Cookie domain based on server variables
255+
* Removes www. if found to allow subdomains
256+
*/
257+
private function setCookieDomain($server)
258+
{
259+
$host = "";
260+
if (array_key_exists('HTTP_HOST', $server)) {
261+
$host = $server["HTTP_HOST"];
262+
if(strpos($host, "www.") === 0) {
263+
$host = substr($host, 4);
264+
}
265+
}
266+
$this->cookieDomain = $host;
267+
}
268+
269+
private function getCookieDomain()
270+
{
271+
return $this->cookieDomain;
272+
}
273+
250274
/**
251275
* Set CrowdHandler session cookie
252276
*/
253277
private function setCookie($cookie)
254278
{
255279
if (!is_null($cookie)) {
256-
setcookie(self::TOKEN_COOKIE, $cookie, 0, '/', '', $this->debug ? false: true);
280+
setcookie(self::TOKEN_COOKIE, $cookie, 0, '/', $this->getCookieDomain(), $this->debug ? false: true);
257281
$this->debug('Setting cookie '.$cookie);
258282
}
259283
}

0 commit comments

Comments
 (0)