After using js to encode the url we use PHP inverse solution and use PHP to realize js escape function summary

  • 2020-03-31 20:14:40
  • OfStack

Smarty can encode urls directly, such as < ! -- {$var | urlencode} - >
But it doesn't seem to be in smarttemplate, because links are submitted by js, not form submissions, so you can't code them automatically.
Solution: js is used to escape the Chinese character in the URL.
< A href = "" onclick =" window. The open (' product_list. PHP? P_sort = '+ escape (' PHP development resource network'));" >
The effect after clicking the link:
Reference: http://127.0.0.1/shop/product_list.php? P_sort = u6E90 u8D44 u53D1 u5F00 PHP % % % % % u7F51
With this effect generated, it is clear that PHP urldecode() or base64_decode() cannot be reversed.
To solve the problem, write an inverse solution function in PHP:
 
function js_unescape($str) 
{ 
$ret = ''; 
$len = strlen($str); 
for ($i = 0; $i < $len; $i++) 
{ 
if ($str[$i] == '%' && $str[$i+1] == 'u') 
{ 
$val = hexdec(substr($str, $i+2, 4)); 
if ($val < 0x7f) $ret .= chr($val); 
else if($val < 0x800) $ret .= chr(0xc0|($val>>6)).chr(0x80|($val&0x3f)); 
else $ret .= chr(0xe0|($val>>12)).chr(0x80|(($val>>6)&0x3f)).chr(0x80|($val&0x3f)); 
$i += 5; 
} 
else if ($str[$i] == '%') 
{ 
$ret .= urldecode(substr($str, $i, 3)); 
$i += 2; 
} 
else $ret .= $str[$i]; 
} 
return $ret; 
} 

Note that the JS encoding is automatically converted to utf-8, so the encoding must be converted to get the correct result, otherwise it will be scrambled.
The code is as follows:
Print iconv (' utf-8 ', 'gb2312, js_unescape ($_REQUEST [' p_sort']));
At this point we have successfully solved js escape.
As follows:
Quote: PHP development resources network
In addition, I found a function that USES PHP to implement js escape code:
 
function phpescape($str) 
{ 
$sublen=strlen($str); 
$retrunString=""; 
for ($i=0;$i<$sublen;$i++) 
{ 
if(ord($str[$i])>=127) 
{ 
$tmpString=bin2hex(iconv("gb2312","ucs-2",substr($str,$i,2))); 
//$tmpString = substr ($tmpString, 2, 2). The substr ($tmpString, 0, 2); This might open under window
$retrunString.="%u".$tmpString; 
$i++; 
} else { 
$retrunString.="%".dechex(ord($str[$i])); 
} 
} 
return $retrunString; 
} 

Have you ever had this problem?

Related articles: