What does escape mean, implementing JavaScript's Escape () function code in PHP

Source: Internet
Author: User
In Javascript, you can use the Escape/unescape () and Eval_r () functions for a simple transcoding process, so that the normal URL URLs look weird, making annoying search crawlers unable to identify the resources you want to hide.

Here, it is generally necessary to pre-encode the normal string into a format that the JavaScript unescape () function can interpret, and in PHP, for example, you can use the following function to implement the same functionality as the Escape () function in javascript:

The code is as follows:

<?php function Escapetohex ($string, $encoding = UTF-8) {$return =; for ($x = 0; $x < Mb_strlen ($string, $encoding) ; $x + +) {$str = Mb_substr ($string, $x, 1, $encoding), if (strlen ($STR) > 1) {//multibyte character $return. =%u. Strtoupper (bin2h Ex (Mb_convert_encoding ($str, UCS-2, $encoding))); } else {$return. =%. Strtoupper (Bin2Hex ($STR));}} return $return; }?>


Suppose we want to hide the following address: http://www.php.cn/
We can use the following script to implement:

The code is as follows:

<?php//Please include your own escapetohex () function definition $test = document.write (\<a href= "http://www.dirk.sh/assets/uploaded/ Thisistest.pdf ">test</a>\); echo <script language= "Javascript" >eval_r (unescape (". Escapetohex ($test). ")) </script>;?>


If you look at the source code of the page, you will see (because it is too long, so you have a manual branch, the actual result should be a complete line):

The code is as follows:

<script language= "Javascript" >eval_r (unescape ("%64%6f%63%75%6d%65%6e%74%2e%77 \%72%69%74%65%28%27%3c%61%20 %68%72%65%66%3d%22%68%74%74%70%3a%2f%2f%77%77%77 \%2e%64%69%72%6b%79%65%2e%6e%65%74%2f%75%70%6c%6f%61%64%65%64% 2f%74%68%69%73 \%69%73%74%65%73%74%2e%70%64%66%22%3e%74%65%73%74%3c%2f%61%3e%27%29 ")) </script>


The page displayed in the browser is not the same as the normal HTML.
Note:
1, the second parameter of the Escapetohex () function ($encoding) represents the encoding of the string that you pass in, the default is UTF-8, if you use other encodings, you should explicitly specify in the calling function;
2, Unescape () is objected to in the ECMAScript V3 specification, the specification recommends using the new alternative function decodeuricomponent (), but I have been tested to find that decodeuricomponent () The function has problems with multibyte character processing, so the unescape () function is still used.
3, in principle, the above method is only to prevent search crawlers to get the resource address you think you need to keep confidential, browse the page under Javascript-enabled browser, and see the rendering is identical without enabling this protection mechanism.

  • 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.