Forum Moderators: coopster
function setcookie($key, $value, ....)
{
$value = urlencode($value);
header("Set-Cookie: $key=$value", false);
}
function parse_cookies()
{
$a = explode(";" $_SERVER['HTTP_COOKIE']);
foreach ($a as $b) {
list($k, $v) = explode("=", $b, 2);
$_COOKIE[urldecode(trim($k))] = $_COOKIE[urldecode[$v]);
}
}
this is how php process the cookies
suppose my internal encoding is GBK
if we setcookie($key, $value); the $value is urlencoded by setcookie, if the browser do a unescape, it get string which encoding is GBK, not Javascript Internal Encoding
now the question is:
method 1:
do u do a iconv to translate GBK into UCS-2LE before setcookie? however, it's still urlencoded by setcookie() before send to client
and the client get a format like %ab%cd%ef, if javascript do a unescape on it, it's latin1 encoding, not UCS-2LE
(each word is in the range of 0x00-0xFF)
so we have to implement a function to decode it into UCS-2LE?
again, in php, we have to iconv $_COOKIE[$key] from UCS-2LE into GBK encoding
method 2:
in php side, implement a mysetcookie() ourself
and do a js-like escape() then do header(.., false);
in js side, just do a simple unescape() to get the value, or escape() when save
but for php to read the cookie, seems that we have to do a js-like unescape() in mygetcookie()
so... is there any better ways?
How else can a cookie be generated if a client can only take html and javascript commands?
Cookies can be sent within the headers, that is how php works certainly. That is in fact the main way cookies are sent - javascript accounts for only a small proportion of all cookies sent. An example of headers doing cookie manipulaton:
HTTP/1.1 302 Found
Date: Sat, 26 Jul 2003 12:26:33 GMT
Server: Apache/1.3.26 (Unix) PHP/4.3.0
X-Powered-By: PHP/4.3.0
Set-Cookie: mysession=61f56s1df51sdf51; expires=Fri, 26-Jul-02 12:26:32 GMT; domain=.example.co.uk
Location:
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html
To send that header i can directly use:
header("Set-Cookie: mysession=61f56s1df51sdf51; expires=Fri, 26-Jul-02 12:26:32 GMT; domain=.example.co.uk");
or use setcookie(), which just formats it for me