Creating cookies with PHP for Widows and Macintosh

I discovered a little quirk today when using a php script to write a cookie for users viewing preferences. Apparently you need to write them differently for both mac and pc. It drove me crazy trying to troubleshoot this.

One way to write the cookie is as so…
setcookie(‘viewingPreferences’, $myCookieVars, time()+60*60*24*365*10, ‘/’, ‘.www.darkstarmedia.net’);


But on windows it doesn’t work (tested on IE, Firefox and Crome). It wants it written as so…

setcookie(‘viewingPreferences’, $myCookieVars, time()+60*60*24*365*10, ‘/’);
without the website…

So I came up with this little script with a little help from google…

$user_agent = $_SERVER[‘HTTP_USER_AGENT’];
if (preg_match(‘/linux/i’, $user_agent)) {
$platform = ‘linux’;
}
elseif (preg_match(‘/macintosh|mac os x/i’, $user_agent)) {
$platform = ‘mac’;
}
elseif (preg_match(‘/windows|win32/i’, $user_agent)) {
$platform = ‘windows’;
}

if($platform == “windows”) {
setcookie(‘viewingPreferences’, $myCookieVars, time()+60*60*24*365*10, ‘/’);
} else {
setcookie(‘viewingPreferences’, $myCookieVars, time()+60*60*24*365*10, ‘/’, ‘.www.darkstarmedia.net’);
}

I don’t have a Linux computer to test on, so I am just going to assume it works properly like it does on a mac.