Skip to content

Commit

Permalink
Cookie for sessions should now get set properly for Chrome Browsers w…
Browse files Browse the repository at this point in the history
…hen server uses IP address instead of domain name

Fix for issue #465
  • Loading branch information
eSilverStrike committed Feb 25, 2022
1 parent 4e98534 commit 9ad6df3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
48 changes: 48 additions & 0 deletions system/classes/device.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,30 @@ class Device
const COMPUTER = 'computer';
const MOBILE = 'mobile';
const ALL = 'all';

// Constants for Browsers
const CHROME = 'Chrome';
const OPERA = 'Opera';
const EDGE = 'Edge';
const SAFARI = 'Safari';
const FIREFOX = 'Firefox';
const IE = 'Internet Explorer';
const OTHER = 'Other';

/**
* device type if already checked before
*
* @var string one of self::PHONE, self::TABLET, or self::COMPUTER
*/
private $type;

/**
* browser type if already checked before
*
* @var string one of self::CHROME, self::OPERA, self::EDGE, self::SAFARI, self::FIREFOX, self::IE, or self::OTHER
*/
private $browser;


/**
* Store if mobile device (includes phones and tablets) if already checked before
Expand All @@ -64,6 +81,8 @@ class Device
*/
public function __construct()
{
global $_SERVER;

// Include and instantiate the class.
$detect = new Mobile_Detect;

Expand All @@ -80,6 +99,25 @@ public function __construct()
$this->is_mobile = false;
$this->type = self::COMPUTER;
}

// Determine Browser Type
$user_agent = $_SERVER['HTTP_USER_AGENT'];

if (strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/')) {
$this->browser = self::OPERA;
} elseif (strpos($user_agent, 'Edge')) {
$this->browser = self::EDGE;
} elseif (strpos($user_agent, 'Chrome')) {
$this->browser = self::CHROME;
} elseif (strpos($user_agent, 'Safari')) {
$this->browser = self::SAFARI;
} elseif (strpos($user_agent, 'Firefox')) {
$this->browser = self::FIREFOX;
} elseif (strpos($user_agent, 'MSIE') || strpos($user_agent, 'Trident/7')) {
$this->browser = self::IE;
} else {
$this->browser = self::OTHER;
}
}

/**
Expand Down Expand Up @@ -126,4 +164,14 @@ public function type()
{
return $this->type;
}

/**
* What type of browser is being used on the device?
*
* @return string (self::CHROME, self::OPERA, self::EDGE, self::SAFARI, self::FIREFOX, self::IE, self::OTHER)
*/
public function browser()
{
return $this->browser;
}
}
14 changes: 7 additions & 7 deletions system/lib-sessions.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@
if (substr($server[1], 0, 4) === 'www.') {
$_CONF['cookiedomain'] = substr($server[1], 3);
} else {
// Issue #465
// local names part works fine and if domain is an ip it works fine for browsers Firefox and Edge
// If browser is Chrome if an IP cookie domain needs to be set to ''.
// Unfortantly Firefox and Edge doesn't work with cookie domain set to ''
// So left as is with Google Chrome sessions not working properly if IP is the cookie domain
if (strpos($server[1], '.') === false) {
// e.g. 'localhost' or other local names
// Issue #465 - Fix for cookie not getting set for Chrome browsers if domain is IP address
if (($_DEVICE->browser() == 'Chrome') && (filter_var($server[1], FILTER_VALIDATE_IP) || filter_var($server[1], FILTER_FLAG_IPV6))) {
// For Chrome browsers when IP detected instead of domain name
$_CONF['cookiedomain'] = '';
} elseif (strpos($server[1], '.') === false) {
// For 'localhost' or other local names
$_CONF['cookiedomain'] = '';
} else {
// For regular domain names or IPs
$_CONF['cookiedomain'] = '.' . $server[1];
}
}
Expand Down

0 comments on commit 9ad6df3

Please sign in to comment.