JS Set Cookie
| From: | Michael Stearne | Date: | Wed, 24 Nov 1999 04:48:58 +0000 |
| Subject: | JS Set Cookie | ||
| Groups: | php.version4 | ||
| Request: | Send a blank email to php-version4+get-6819@lists.php.net to get a copy of this message | ||
A lot of times people need to set cookies after the headers have been
sent on a page. The function below will generate a Javascript style
cookie that can be used in this situation, is there anyway this
functionality might be added as a PHP4 function?
Michael
///////////////////////////////////////////////////////////////////////
function JSSetCookie($CName,$CValue,$CExpr=FALSE){
// $CName is the name of the cookie
// $CValue is the value to be stored in the cookie
// $CXpr is in years
// This function will allow cookies to be set even after
// HTML has been printed on the page. It is a supplement to the
// SetCookie function when you have to output text before a cookie is
set.
if($CExpr!=FALSE){
$CookieString="astr= '$CName' + '=' + '$CValue' +
';expires=' + expr +
';path=/';";
$Cookie.="\n<script language=\"javascript\">\n";
$Cookie.='function makeYearExpDate(yr){
var expire = new Date();
expire.setTime(expire.getTime() + ((yr *365) *24 * 60 * 60 * 1000));
expire = expire.toGMTString()
return expire
}
expr =makeYearExpDate('.$CExpr.');';
$Cookie.="\n".$CookieString."\n";
$Cookie.="document.cookie=astr;\n</script>\n";
}else{
$Cookie.="\n<script language=\"javascript\">\n";
$Cookie.="document.cookie='$CName=$CValue;path=/';";
$Cookie.="\n</script>\n";
}
echo $Cookie;
}