Categories
technical

How to unescape cookie value in php which was escaped in javascript

I am a newbie study php recently, I want operate cookie by Javascript need escape and  unescape the value:

<script Language=Javascript>
function SetCookie(name,value)//两个参数,一个是cookie的名子,一个是值
{

var Days = 30; //此 cookie 将被保存 30 天

var exp = new Date(); //new Date(“December 31, 9998”);
exp.setTime(exp.getTime() + Days*24*60*60*10000);
document.cookie = name + “=”+ escape (value) + “;expires=” + exp.toGMTString();
}
function getCookie(name)//取cookies函数
{
var arr = document.cookie.match(new RegExp(“(^| )”+name+”=([^;]*)(;|$)”));
if(arr != null) return unescape(arr[2]); return null;

}

</script>

Sometime we need operate cookie in PHP, How to unescape cookie value in php which was escaped in javascript? I were google the solution hardly, finally I get the solution below :

<?php

function unescape($s) {
$s= preg_replace(‘/%u(….)/’, ‘&#x$1;’, $s);
$s= preg_replace(‘/%(..)/’, ‘&#x$1;’, $s);
return $s;
}
?>

reference link from here

mark it and for your reference if you can google that 🙂

2 replies on “How to unescape cookie value in php which was escaped in javascript”

Тerrific post however I was ωanting to know іf you
could wrіte a litte more on this subject? Ι’d be very grateful if you could elaborate a little bit further. Many thanks!

Leave a Reply to Shawn Cancel reply

Your email address will not be published. Required fields are marked *