Common escape functions in PHP

Source: Internet
Author: User
Tags sql injection

This article mainly introduces the commonly used escape functions, security functions in PHP, use these functions can filter most common attack means, such as SQL injection, the need for friends can refer to the

1. addslashes

Addslashes escapes the special characters in the SQL statement, including ('), ("), (), (NUL) four characters, this function is used when the DBMS does not have its own escape function, but if the DBMS has its own escape function, then the original function is recommended. For example, MySQL has a mysql_real_escape_string function to escape SQL. Note that before PHP5.3, MAGIC_QUOTES_GPC is turned on by default, mainly in $get, $POST, $COOKIE to perform addslashes operations, so there is no need to repeat addslashes on these variables, or double Escaping's. However, MAGIC_QUOTES_GPC has been abandoned in PHP5.3, has been removed from the PHP5.4, if the latest version of PHP can not worry about this problem. Stripslashes is the unescape function of the addslashes.

2. Htmlspecialchars

Htmlspecialchars a few special words in HTML escape into HTML Entity (format:&xxxx;) Form, including (&), ('), ("), (<), (>) five characters.

& (And) => &

"(double quotes) =>" (when Ent_noquotes is not set)

' (single quotation mark) => ' (when Ent_quotes set)

< (less than) => <

> (greater than number) => >

Htmlspecialchars can be used to filter $get, $POST, $COOKIE data, and prevent XSS. Note the Htmlspecialchars function only escapes HTML characters that are considered to be a security risk, and use htmlentities if you want to escape all the characters that can be escaped by HTML. Htmlspecialchars_decode is the decode function of the htmlspecialchars.

3. Htmlentities

Htmlentities escapes the content that can be escaped in HTML into HTML Entity. Html_entity_decode is the decode function of the htmlentities.

4. mysql_real_escape_string

Mysql_real_escape_string will invoke MySQL's library function mysql_real_escape_string, pair (x00), (n), (R), (), ('), (x1a) to escape, that is, add a backslash () in front, Prevent SQL injection. Note that you don't need to call stripslashes to unescape when reading database data, because these backslashes are added when the database executes SQL, and when the data is written to the database, the backslash is removed, so the content written to the database is the original data, Does not have a backslash in front.

5. Strip_tags

Strip_tags will filter out nul,html and PHP tags.

6. Conclusion

PHP's own security function does not completely avoid XSS, and it is recommended that you use HTML purifier

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.