Home >Backend Development >PHP Tutorial >Optimized addslashes function

Optimized addslashes function

WBOY
WBOYOriginal
2016-07-25 09:07:12941browse
  1. function daddslashes($string, $force = 0) {
  2. if(!$GLOBALS['magic_quotes_gpc'] || $force) {
  3. if(is_array($string)) {
  4. foreach($string as $key => $val) {
  5. $string[$key] = daddslashes($val, $force);
  6. }
  7. } else {
  8. $string = addslashes($string);
  9. }
  10. }
  11. return $string ;
  12. }
Copy code

Instructions: string addslashes ( string str ) Returns a string with backslashes added in front of certain characters for database query statements, etc. These characters are single quote (’), double quote (”), backslash () and NUL (NULL character).

An example of using addslashes() is when you are entering data into a database. For example, inserting the name O’reilly into the database requires escaping it. Most databases use as escape character: O’reilly. This puts the data into the database without inserting extra . When the PHP directive magic_quotes_sybase is set to on, it means that inserting ' will be escaped with '.

By default, the PHP instruction magic_quotes_gpc is on, which mainly automatically runs 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. When encountering this situation, you can use the function get_magic_quotes_gpc() to detect it.



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