Home  >  Article  >  Backend Development  >  Analysis of PHP's eight major security functions_PHP tutorial

Analysis of PHP's eight major security functions_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:13:30781browse

Analysis of PHP’s eight major security functions

In the modern Internet, we often have to get input data from users around the world. However, we all know "never trust user-entered data." Therefore, in various web development languages, functions are provided to ensure the security of user input data. In PHP, there are some very useful and convenient functions that can help your website prevent problems like SQL injection attacks, XSS attacks, etc. Of course, when writing PHP code, IDEs such as: PhpStorm and Zend Studio will highlight functions to ensure developers' use. Some people also use code obfuscation tools to protect these functions or codes (such as: Zend Guard). These methods are all It is used to ensure the use and safety of PHP functions. Today we are mainly going to take a look at how these functions are defined and work.

Analysis of PHPs eight major security functions_PHP tutorial

1. mysql_real_escape_string()

This function is very helpful for preventing SQL injection attacks in PHP. It adds a "backslash" to special characters, such as single quotes and double quotes, to ensure that the user's input has been processed before using it to query. It's safe now. But you should note that you are using this function while connected to the database.

But now the mysql_real_escape_string() function is basically no longer needed. All new application development should use libraries like PDO to operate the database. In other words, we can use ready-made statements to prevent SQL injection attacks.

2. addslashes()

This function is very similar to mysql_real_escape_string() above. But be careful not to use this function when the value of magic_quotes_gpc in the setting file php.ini is "on". By default, magic_quotes_gpc is on, automatically running addslashes() on all GET, POST and COOKIE data. Do not use addslashes() on strings that have been escaped by magic_quotes_gpc, as this will result in double escaping. You can check the value of this variable through the get_magic_quotes_gpc() function in PHP.

3. htmlentities()

This function is very useful for filtering user input data. It can convert characters into HTML entities. For example, when the user enters the character "

4. htmlspecialchars()

Some characters in HTML have special meanings. If you want to reflect such meanings, they must be converted into HTML entities. This function will return the converted string. For example, '&'amp will be converted to '&' .

5. strip_tags()

This function can remove all HTML, JavaScript and PHP tags from the string. Of course, you can also make some specific tags appear by setting the second parameter of the function.

6. md5()

Some developers store very simple passwords, which is not good from a security perspective. The md5() function can generate a 32-character md5 hash of a given string, and the process is irreversible, i.e. you The original string cannot be obtained from the result of md5().

7. sha1()

This function is similar to md5() above, but it uses a different algorithm and produces a 40-character SHA-1 hash (md5 produces a 32-character hash).

8. intval()

Don’t laugh, I know this is not a safety-related function, it is converting variables into integer types. However, you can use this function to make your PHP code more secure, especially when you are parsing data like id, age.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/915113.htmlTechArticleAnalysis of PHP’s eight major security functions In the modern Internet, we often have to obtain input data from users around the world. However, we all know you can never trust those user-entered data...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn