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?