php和js互相之间读写cookie,乱码的问题

我的php代码:

//common.php
/**
* 将键值保存到cookie中
* @param string $key 键
* @param mixed 值
*/
function save_cookie($key, $val) {
$time = time() + 3600 * 24 * 365; //1年
setcookie($key, trim($val), $time);
}
/**
* 从cookie中读取值
* @param string $key 键
* @return mixed 值
*/
function load_cookie($key) {
$result = isset($_cookie[$key]) ? trim($_cookie[$key]) : ”;
return $result;
}

我的js代码:

//cookie.js
function setcookie(name, value) {
var str = name + ‘=’ + encodeuricomponent(value) + ‘;’; //escape //encodeuri //encodeuricomponent
str += ‘expires=’ + new date(new date().gettime() + 3600 * 24 * 365 * 1000).toutcstring() + ‘;’; //togmtstring //toutcstring
console.log(str);
document.cookie = str; //”user_name=szuzsq;expires=thu, 10 may 2018 11:01:27 gmt;”
}
function getcookie(name) {
var str = document.cookie; //”user_name=szuzsq; password=4d9c5b6c43ebfb7f43bad749c20486d5″
if(!str || str.length

Posted in 未分类

发表评论