Home  >  Article  >  Backend Development  >  Addslashes function and SQL injection prevention in PHP, addslashessql_PHP tutorial

Addslashes function and SQL injection prevention in PHP, addslashessql_PHP tutorial

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

The addslashes function in php and sql anti-injection, addslashessql

The example in this article describes the addslashes function and SQL injection prevention in php. Share it with everyone for your reference. The specific analysis is as follows:

addslashes can automatically add \\\ to single quotes and double quotes, so that we can safely store data in the database without being exploited by hackers. The parameter 'a..z' defines that all uppercase and lowercase letters will be converted Meaning, the code is as follows:

Copy code The code is as follows:
echo addcslashes('foo[ ]','a..z'); //Output: foo[ ]
$str="is your name o'reilly?"; //Define a string, including characters that need to be escaped
echo addslashes($str); //Output the escaped string

Definition and usage: The addslashes() function adds a backslash before the specified predefined characters.

These predefined characters are: single quote ('), double quote ("), backslash (), null

Syntax: addslashes(string), of course this function is safer, the example code is as follows:

Copy code The code is as follows:
$str="test"; //Definition String containing special characters
$new=htmlspecialchars($str,ent_quotes); //Perform conversion operation
echo $new; //Output the conversion result
//But you need to use
when outputting $str="jane & 'tarzan'"; //Define html string
echo html_entity_decode($str); //Output the converted content
echo "
";
echo html_entity_decode($str,ent_quotes); //Content output with optional parameters

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/912673.htmlTechArticleaddslashes function and sql anti-injection in php, addslashessql This article describes the addslashes function and sql anti-injection in php. Share it with everyone for your reference. The specific analysis is as follows: addslashes can...
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