Read an article on the web about JS and PHP coding _php Tutorial

Source: Internet
Author: User
Workaround: Use JS to the URL of the Chinese character escape code.



When you click the link, the effect is:

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


The result is that the UrlDecode () or Base64_decode () in PHP is clearly not solvable.

Workaround, write an inverse 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 ($s TR, $i, 3));
$i + = 2;
}
Else $ret. = $str [$i];
}
return $ret;
}




Note that the JS encoding will automatically be converted into UTF-8, so the encoding must be converted to get the correct results, otherwise it will be garbled in Chinese.

The code is as follows:

Print iconv (' utf-8 ', ' gb2312 ', Js_unescape ($_request[' p_sort '));



To this we have successfully reversed the JS escape code.
As follows:
References: PHP Development Resource Network


In addition, I found a PHP implementation of the Escape encoding function:

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); window may open this
$retrunString. = "%u". $tmpString;
$i + +;
} else {
$retrunString. = "%". Dechex (Ord ($str [$i]);
}
}
return $retrunString;
}



Have you ever encountered such a problem?

http://www.bkjia.com/PHPjc/738515.html www.bkjia.com true http://www.bkjia.com/PHPjc/738515.html techarticle Workaround: Use JS to the URL of the Chinese character escape code. ahref= "" onclick= "window.open (' product_list.php?p_sort= ' +escape (' PHP Development Resource Network ');" When you click on the link to do this: ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.