Common escape functions in PHP

Source: Internet
Author: User

1. addslashes

Addslashes escapes special characters in SQL statements, including ('), ("), (), (NUL, this function is used when the DBMS does not have its own escape function. However, if the DBMS has its own escape function, it is recommended to use the original function. For example, MySQL has the mysql_real_escape_string function to escape SQL. Note that magic_quotes_gpc is enabled by default before PHP5.3. It is mainly used to perform the addslashes operation on $ GET, $ POST, and $ cookies. Therefore, you do not need to repeatedly call addslashes on these variables, otherwise, it will be double escaping. However, magic_quotes_gpc has been deprecated in PHP5.3 and removed from PHP5.4. If you use the latest PHP version, do not worry about this problem. Stripslashes is the unescape function of addslashes.

2. htmlspecialchars

Htmlspecialchars converts several special characters in HTML into HTML Entity (Format: & xxxx;), including (&), ('), ("), (<), (>) five characters.

& (AND) => &
"(Double quotation marks) =>" (when ENT_NOQUOTES is not set)
'(Single quotes) =>' (when ENT_QUOTES is set)
<(Minor sign) => <
> (Greater than the number) =>
Htmlspecialchars can be used to filter $ GET, $ POST, and $ COOKIE data to prevent XSS attacks. Note that the htmlspecialchars function only escapes HTML characters that are considered to have security risks. to escape all HTML characters that can be escaped, use htmlentities. Htmlspecialchars_decode is the decode function of htmlspecialchars.

3. htmlentities

Htmlentities converts the escaped content in HTML to HTML Entity. Html_entity_decode is the decode function of htmlentities.

4. mysql_real_escape_string

Mysql_real_escape_string will call the MySQL library function mysql_real_escape_string, escape (\ x00), (\ n), (\ r), (), ('), (\ x1a, add a backslash () to prevent SQL injection. Note that you do not need to call stripslashes for unescape when reading database data, because these backslashes are added when the database executes SQL statements, when data is written to the database, the backslash will be removed. Therefore, the content written to the database is the raw data, and the backslash will not be added before.

5. strip_tags

Strip_tags filters NUL, HTML, and PHP tags.

6. Conclusion

PHP built-in security functions cannot completely avoid XSS. We recommend that you use HTML Purifier.

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.