Home  >  Article  >  Backend Development  >  Detailed explanation of php special character escaping

Detailed explanation of php special character escaping

WBOY
WBOYOriginal
2016-07-25 08:52:021890browse
  1. $html = array( );
  2. $html['username'] = htmlentities($clean['username'], ent_quotes, 'utf-8');
  3. echo "< p>welcome back, {$html['username']}.

    ";
  4. ?>
Copy code

Tips The htmlspecialchars() function is basically the same as the htmlentities() function. Their parameter definitions are exactly the same, except that the escaping of htmlentities() is more thorough. By outputting username to the client via $html['username'], you can ensure that the special characters will not be misinterpreted by the browser. If the username only contains letters and numbers, escaping is actually not necessary, but this reflects the principle of defense in depth. Escaping any output is a very good habit and can dramatically improve the security of your software.

Another common output destination is the database. If possible, the data in the SQL statement needs to be escaped using PHP's built-in functions.

For mysql database users, the best escape function is mysql_real_escape_string(). If the database used does not have PHP's built-in escape function available, addslashes() is the last choice.

Example, correct escaping techniques for mysql database:

  1. $mysql = array( );
  2. $mysql['username'] = mysql_real_escape_string($clean['username']);
  3. $sql = "select *
  4. from profile
  5. where username = '{$mysql['username']}'";
  6. $result = mysql_query($sql);
  7. ?>
Copy code

php filter parameters special characters anti-injection php method to filter illegal and special strings php example: example of special character processing function php code to replace special characters in extra long text



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