note 21155 modified in function.setcookie by john
| From: | john@php.net | Date: | Sat, 30 Nov 2002 16:35:13 +0000 |
| Subject: | note 21155 modified in function.setcookie by john | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-40171@lists.php.net to get a copy of this message | ||
[Editor's Note: The initial post of the code has been erased (since it was buggy). However,
here is the acknowledgement just to maintain credit :)
/************************************************************
FUNCTION SEND_HTCOOKIE
Author: Van Carney (van@Webfreshener.com)
©2000-2002 Webfreshener.com
last rev: 4/21/02 3:56:47 AM
HTTP Cookie generation routine
takes variable number of args and writes cookie to header
per the Netscape Cookies Spec
This user function is written to mimic the built-in setcookie()
function in PHP.
This was done as problems were noticed with that function
when writing cached cookies from within Object Scopes.
PHP would not write a cached cookie when setcookie()
was called inside an object. This function has been tested
and workes quite nicely in multiple scopes.
usage:
send_htCookie(string varname (required),string varval,string expire,
string path, string domain, bool secure);
*************************************************************/
]
Thanks to Van Carney.
But, that function has a little problem in PHP 4.2.0.
So, I did re-write it, below.
/******************************
ysage:
send_htCookie(string varname (required),string varval,int expire,
string path, string domain, bool secure);
******************************/
function send_htCookie() {
$vars=array('varname','varval','expire',
'path','domain','secure');
for ($i=0;$i<func_num_args();$i++) {
${$vars[$i]}=func_get_arg($i);
}
if (!$varname) { return false; }
$COOKIE = "Set-Cookie: $varname=$varval";
if (isset($expire) && ($expire > 0)) {
$COOKIE .= "; EXPIRES=".
gmdate("D, d M Y H:i:s",$expire) .
" GMT";}
if (isset($domain)) { $COOKIE .= "; DOMAIN=$domain"; }
if (isset($path)) { $COOKIE .= "; PATH=$path"; }
if (isset($secure) && $secure>0) { $COOKIE .= "; SECURE"; }
header($COOKIE,false);
return true;
}
--was--
Thanks to Van Carney.
But, that function has a little problem in PHP 4.2.0.
So, I did re-write it, below.
/******************************
ysage:
send_htCookie(string varname (required),string varval,int expire,
string path, string domain, bool secure);
******************************/
function send_htCookie() {
$vars=array('varname','varval','expire',
'path','domain','secure');
for ($i=0;$i<func_num_args();$i++) {
${$vars[$i]}=func_get_arg($i);
}
if (!$varname) { return false; }
$COOKIE = "Set-Cookie: $varname=$varval";
if (isset($expire) && ($expire > 0)) {
$COOKIE .= "; EXPIRES=".
gmdate("D, d M Y H:i:s",$expire) .
" GMT";}
if (isset($domain)) { $COOKIE .= "; DOMAIN=$domain"; }
if (isset($path)) { $COOKIE .= "; PATH=$path"; }
if (isset($secure) && $secure>0) { $COOKIE .= "; SECURE"; }
header($COOKIE,false);
return true;
}
http://www.php.net/manual/en/function.setcookie.php