We know that the role of the addslashes () function is to add backslashes before some of the predefined characters in the input string. So how does it relate to our anti-SQL injection? What is SQL injection?
SQL injection attacks are the most common means by which hackers attack websites. If your site does not use strict user input validation, it is often vulnerable to SQL injection attacks. SQL injection attacks typically submit bad data or query statements to the site database
, it is likely that the records in the database will be exposed, changed, or deleted. This article mainly introduces PHP using addslashes function to implement SQL anti-injection example , through The example describes the use of the addslashes function for SQL anti-injection.
Example
Parameter ' a '. Z ' defines that all uppercase and lowercase letters are escaped and the code is as follows:
echo addcslashes (' foo[] ', ' A. Z '); Output: foo[] $str = "Is your name O ' Reilly?"; Defines the string, which includes the character that needs to be escaped echo addslashes ($STR); Outputs an escaped string
Definition and Usage:
The Addslashes () function adds a backslash before the specified predefined character.
These predefined characters are: Single quotation mark ('), double quotation mark ("), backslash (), NULL
Grammar:
Addslashes (String)
Of course this function is more secure and the instance code is as follows:
$str = "<a href= ' test ' >test</a>"; Defines a string that contains special characters $new =htmlspecialchars ($str, ent_quotes); Perform the conversion operation Echo $new; Output conversion result//Output $STR = "Jane & ' Tarzan '"; Defines an HTML string echo html_entity_decode ($STR); Output the converted content echo "<br/>"; Echo Html_entity_decode ($str, ent_quotes); Content with optional parameter output
This article has a good reference value for PHP security program design. But using addslashes to prevent SQL injection hackers is a way to bypass, and not so secure. Many PHP programmers still rely on addslashes to prevent SQL Note
, it is recommended to strengthen Chinese to prevent SQL injection check. The problem with addslashes is that hackers can use 0xbf27 instead of single quotes, and addslashes just modifies 0xbf27 to 0xbf5c27 to become a valid multibyte character,