Forum Moderators: coopster

Message Too Old, No Replies

how to save multibyte chars in cookie while letting php & js to access

what's the best way?

         

Xuefer

10:56 am on Jul 25, 2003 (gmt 0)

10+ Year Member



php do a urldecode() when parsing $_COOKIE
and urlencode() for setcookie()
this keeps character encoding(e.g.: GBK BIG5) in cookies
but it's not possible for js to access it
js can only process UTF-8 and UCS-2LE

vincevincevince

6:22 pm on Jul 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



it's going in the other direction - but how about [forums.devshed.com...]

Xuefer

2:55 am on Jul 26, 2003 (gmt 0)

10+ Year Member



hrm... it's not that case

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?

vincevincevince

10:00 am on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think the second way is the way to go. However, maybe you could pass the values to javascript within the body of the page, then javascript handle the cookies totally?

Xuefer

11:03 am on Jul 26, 2003 (gmt 0)

10+ Year Member



hrm,.. that seems a good idea, thanks vincevincevince

it make me needn't to use iconv for php to setcookie, let browser to "read" it
but sill need iconv to prase cookies(this pass may not be avoid)

but some of my page don't require js for those js-disabled browser

amznVibe

11:14 am on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wait, all cookie processing by a client browser has to use javascript.
Even if you use php or asp or whatever to generate the code, eventually
javascript has to be sent to the client right? How else can a cookie be
generated if a client can only take html and javascript commands?

vincevincevince

11:26 am on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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