我hava json字符串被附加到url然后放入php cookie。
//redirect url with short json string appended
echo "<script type=\"text/javascript\">document.location = \"site/temp_process.php?ui=". urlencode(json_encode($user_information)) ." \";</script>";//$user_information is php assoc. array
$user_information = urldecode($_REQUEST['ui']); //user information
setcookie('isco', $user_information, time()+60*60*24*30);
然后使用this javascript function阅读Cookie:
cookie = readCookie("isco");
cookie = decodeURIComponent(cookie);
function readCookie(name) {
var cookiename = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++){
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(cookiename) == 0) return c.substring(cookiename.length,c.length);
}
return null;
}
但是无论我删除javascript decodeURIComponent
还是切换php urlencode/urldecode
rawurlencode/rawurldecode
,我仍然会获得显示为+
符号的空格。我怎么能避免这个?
非常感谢!