Home >Backend Development >PHP Tutorial >About how js and php handle url encoding_PHP tutorial

About how js and php handle url encoding_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:37:09911browse

Solution: Use js to escape encode the Chinese characters in the URL.

Copy code The code is as follows:

The time after clicking the link like this:

Quote: http://127.0.0.1/shop/product_list.php?p_sort=PHP%u5F00%u53D1%u8D44%u6E90%u7F51

Such an effect is generated. It is obvious that it cannot be decoded using PHP's urldecode() or base64_decode().

Solution, use PHP to write an inverse solution function:

Copy the code The code is as follows:
Function js_unescape ($ str) {
$ RET = '';
         $len = strlen($str); [$i+1] == 'u'){
                                                                                                                                                                                                                                                                   through 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));
                          += 5;
}

else if ($str[$i] == '%'){
$ret .= urldecode(substr($str, $i, 3));
$i + = 2;
} Else $ RET. = $ Str [$ i]; }

Return $ Ret;
}



Note that JS encoding will be automatically converted to UTF-8, so encoding conversion must be performed to get the correct result, otherwise the Chinese characters will be garbled.


Copy code

The code is as follows:

print iconv('utf-8', 'gb2312', js_unescape($_REQUEST[ 'p_sort']));
At this point we have successfully decoded the escape encoding of js.

In addition, I found a function that uses PHP to implement escape encoding of js:

Copy the code

The code is as follows:

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).substr($tmpString,0,2); You may need to open this item under window
$retrunString.="%u".$tmpString;
$i++;
} else {
         $retrunString.="%".dechex(ord($str[$i]));
}}
return $retrunString;
}






http://www.bkjia.com/PHPjc/736804.html
www.bkjia.com

true

TechArticleSolution: Use js to escape encode the Chinese characters in the URL. Copy the code as follows: a href="" onclick="window.open('product_list.php?p_sort='+escape('PHP Development Resource Network'));"...
Previous article:Analysis of the causes of PHP program vulnerabilities and explanation of prevention methods_PHP tutorialNext article:Analysis of the causes of PHP program vulnerabilities and explanation of prevention methods_PHP tutorial

Related articles

See more