在PHP中先用escape函数编码,到客户端时利用js中的unescape解码。
escape函数如下:
function escape($str)
{
preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$r);
$ar = $r[0];
foreach($ar as $k=>$v)
{
if(ord($v[0]) < 128)
$ar[$k] = rawurlencode($v);
else
$ar[$k] = "%u".bin2hex(iconv("GB2312","UCS-2",$v));
}
return join("",$ar);
}
例子:test.php
<?php
function escape($str) {
preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$r);
$ar = $r[0];
foreach($ar as $k=>$v) {
if(ord($v[0]) < 128)
$ar[$k] = rawurlencode($v);
else
$ar[$k] = "%u".bin2hex(iconv("GB2312","UCS-2",$v));
}
return join("",$ar);
}
$name = escape("深圳人");
setcookie("name", $name);
?>
<scrīpt>
function get_cookie(name)
{
var result = null;
var myCookie = document.cookie + ";";
var searchName = name + "=";
var startOfCookie = myCookie.indexOf(searchName);
var endOfCookie;
if (startOfCookie != -1)
{
startOfCookie += searchName.length;
endOfCookie = myCookie.indexOf(";",startOfCookie);
result = unescape(myCookie.substring(startOfCookie, endOfCookie));
}
return result;
}
</scrīpt>
<scrīpt>
document.write("js:" + unescape(getCookie("name")));
</scrīpt>
另外还有一个方法:
在PHP5中,可以使用setrawcookie()函数来代替,它是在设置cookie值的时候没有编码的,所以在设置cookie的时候也不用先用escape函数编码,这时JS也能直接读取cookie的值
来源:
http://hi.baidu.com/yeakyang/blog/item/3ffd5243fd25fd1573f05db9.html
调用方法:
escape函数如下:
function escape($str)
{
preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$r);
$ar = $r[0];
foreach($ar as $k=>$v)
{
if(ord($v[0]) < 128)
$ar[$k] = rawurlencode($v);
else
$ar[$k] = "%u".bin2hex(iconv("GB2312","UCS-2",$v));
}
return join("",$ar);
}
例子:test.php
<?php
function escape($str) {
preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$r);
$ar = $r[0];
foreach($ar as $k=>$v) {
if(ord($v[0]) < 128)
$ar[$k] = rawurlencode($v);
else
$ar[$k] = "%u".bin2hex(iconv("GB2312","UCS-2",$v));
}
return join("",$ar);
}
$name = escape("深圳人");
setcookie("name", $name);
?>
<scrīpt>
function get_cookie(name)
{
var result = null;
var myCookie = document.cookie + ";";
var searchName = name + "=";
var startOfCookie = myCookie.indexOf(searchName);
var endOfCookie;
if (startOfCookie != -1)
{
startOfCookie += searchName.length;
endOfCookie = myCookie.indexOf(";",startOfCookie);
result = unescape(myCookie.substring(startOfCookie, endOfCookie));
}
return result;
}
</scrīpt>
<scrīpt>
document.write("js:" + unescape(getCookie("name")));
</scrīpt>
另外还有一个方法:
在PHP5中,可以使用setrawcookie()函数来代替,它是在设置cookie值的时候没有编码的,所以在设置cookie的时候也不用先用escape函数编码,这时JS也能直接读取cookie的值
来源:
http://hi.baidu.com/yeakyang/blog/item/3ffd5243fd25fd1573f05db9.html
function QCookie(name, value, options){
if (typeof value != 'undefined') { //set cookie
options = options || {};
if (value === null) { //delete cookie
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
}
else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString();
}
var path = options.path ? '; path=' + options.path : '';
var domain = options.domain ? '; domain=' + options.domain : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
}
}
if (typeof value != 'undefined') { //set cookie
options = options || {};
if (value === null) { //delete cookie
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
}
else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString();
}
var path = options.path ? '; path=' + options.path : '';
var domain = options.domain ? '; domain=' + options.domain : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
}
}
调用方法:
var opts = {path:"/",domain:dmArr[dmLen-2]+'.'+dmArr[dmLen-1], path: "/"};
if(u.length==10){
QCookie("uin",'o'+u,opts);
}
else{
QCookie("uin",'o0'+u,opts);
}
QCookie("skey",'@m8WiH8888,opts);
if(u.length==10){
QCookie("uin",'o'+u,opts);
}
else{
QCookie("uin",'o0'+u,opts);
}
QCookie("skey",'@m8WiH8888,opts);
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/2958/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2010-12-9 11:43
评论列表