Skip to content

Instantly share code, notes, and snippets.

@Danack
Created May 30, 2013 18:16
Show Gist options
  • Save Danack/5679903 to your computer and use it in GitHub Desktop.
Save Danack/5679903 to your computer and use it in GitHub Desktop.
Set cookie function - part of old codebase.
function setCookieVariable($cookieName, $value, $secureOnly = false){
$timeSeconds = 60 * 60 * 24 * 30; //30 days in the future.
// if($GLOBALS['outputStarted'] == true){
// logToFileFatal("Trying to set cookie '$cookieName' but page output has already started. That's bad.");
// header('X-OUTPUT-STARTED: 12345');
// return;
// }
if($secureOnly == true){
if(isHTTPSPage() == false){
logToFileFatal("Setting Cookie [$cookieName] which is meant to be a secure cookie.");
}
}
$domainInfo = DomainManagement::getDomainInfo();
$domainForCookie = '.'.$domainInfo['rootCanonicalDomain']; //leading dot according to http://www.faqs.org/rfcs/rfc2109.html
if($value === null){
logToFileFatal("Trying to set cookie $cookieName to be NULL. Please use unsetCookie to do this, to avoid cookies being accidentally deleted.");
}
if($secureOnly == true){
setcookie($cookieName, $value, time() + $timeSeconds, '/', $domainForCookie, true, true); //We need to set the root domain explicitly
}
else{
setcookie($cookieName, $value, time() + $timeSeconds, '/', $domainForCookie); //We need to set the root domain explicitly
}
//we also set the $_COOKIE variable so that reading the cookie read the same value on this page as subsequent pages.
$_COOKIE[$cookieName] = $value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment