Use php for reverse decoding after url encoding using js and use php to implement the escape Function of js.

Source: Internet
Author: User
The first time I used the template smarttemplate, it was much smaller than smarty, but it was inconvenient.

The first time I used the template smarttemplate, it was much smaller than smarty, but it was inconvenient.

Smarty can directly encode the url, such
But it does not seem to have been found in smarttemplate. Because the link is submitted by js, rather than the form, it cannot be automatically encoded.
Solution: Use js to encode the Chinese characters in the URL.

The following figure shows the validity of the link:
Reference: http: // 127.0.0.1/shop/product_list.php? P_sort = PHP % u5F00 % u53D1 % u8D44 % u6E90 % u7F51
It is obvious that the urldecode () or base64_decode () of PHP cannot be reversed.
Solution: Use PHP to write an anti-solution function:
The Code is as follows:
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 JS encoding will be automatically converted to UTF-8, so the encoding must be converted to get the correct results, otherwise the Chinese garbled.
The Code is as follows:
Print iconv ('utf-8', 'gb2312', js_unescape ($ _ REQUEST ['P _ sort ']);
At this point, we have successfully reversed the js escape code.
As follows:
Reference: PHP development resource network
In addition, I found a function that uses PHP to implement js escape encoding:
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,). substr ($ tmpString,); this option may be enabled in window
$ RetrunString. = "% u". $ tmpString;
$ I ++;
} Else {
$ RetrunString. = "%". dechex (ord ($ str [$ I]);
}
}
Return $ retrunString;
}

Have you ever encountered this problem?

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.