addslashes function and sql anti injection in php

  • 2021-08-03 09:45:50
  • OfStack

In this paper, the addslashes function and sql anti-injection in php are described by examples. Share it for your reference. The specific analysis is as follows:

addslashes can automatically add single quotation marks and double quotation marks, so that we can safely store data in the database without hackers using it. The parameter 'a... z' defines all upper and lower case letters to be escaped. The code is as follows:

echo addcslashes('foo[ ]','a..z'); // Output: foo[ ] 
$str="is your name o'reilly?"; // Defines a string, including the characters that need to be escaped
echo addslashes($str);  // Output 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

Syntax: addslashes (string). Of course, this function is safer. The example code is as follows:

$str="<a href='test'>test</a>"; // Defining Strings Containing Special Characters  
$new=htmlspecialchars($str,ent_quotes);  // Perform a conversion operation
echo $new;           // Output conversion result
// However, it needs to be used when outputting
$str="jane &amp; &#039;tarzan&#039;";  // Definition 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

I hope this article is helpful to everyone's PHP programming.


Related articles: