Processing of JS/PHP characters and URLs

Source: Internet
Author: User
1. Javascript character escaping

JavaScript code involves three functions:Escape, encodeuri, encodeuricomponentCorresponding three decoding functions: Unescape, decodeuri, decodeuricomponent

1,You must use encodeuricomponent when passing parameters so that the combined URL will not be truncated by special characters such.

For example:

<Script language = "JavaScript"> document. Write ('<a href = "http://passport.baidu.com /? Logout & Aid = 7 & U = '+ encodeuricomponent ("http://cang.baidu.com/bruce42") +' "> exit </a & amp; gt; '); </SCRIPT>

2,Encodeuri can be used for URL redirection.

Example: location. href = encodeuri (http://cang.baidu.com/do/s? WORD = China & Ct = 21 );

The urldecode () function can be used to encode characters in PHP.

3,You can use escape when JS uses data.

When encoding Unicode values other than 0-, escape outputs % u *** format. In other cases, escape, encodeuri, and encodeuricomponent have the same encoding result.

Escape unencoded characters are 69: *, +,-,.,/, @, _, 0-9, A-Z, A-Z

Encodeuri is not encoded with 82 characters :!, #, $, &, ', (,), *, +,-,.,/,:,;, = ,?, @,_,~, 0-9, A-Z, A-Z

Encodeuricomponent has 71 unencoded characters :!, ',(,),*,-,.,_,~, 0-9, A-Z, A-Z

2. escape characters in PHP Htmlspecialchars (), htmlspecialchars_decode ()
Htmlentities (), html_entity_decode ()
Urlencode (); urldecode ()
Rawurlencode (); rawurldecode ()

3. Note 1. store raw data in dB; 2. when the access layer obtains the DB data, it needs to perform htmlspecialchars for the string data and URL to be displayed on the page, so that the script can be displayed normally rather than executed; 3. if the access layer (such as PHP test) does not escape the output data, you need to escape the output data before the page test is displayed. It is only used for filling the page with JS, and the top character before filling is specified for escape replacement.

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.