Home >Backend Development >PHP Tutorial >Analysis of the difference between PHP function addslashes and mysql_real_escape_string

Analysis of the difference between PHP function addslashes and mysql_real_escape_string

WBOY
WBOYOriginal
2016-07-25 08:53:23932browse
  1. CREATE TABLE users(
  2. username VARCHAR(32) CHARACTER SET GBK,
  3. password VARCHAR(32) CHARACTER SET GBK,
  4. PRIMARY KEY(username)
  5. );
Copy code

Example, simulation only What happens when query data is escaped using addslashes (or magic_quotes_gpc):

  1. $mysql = array();
  2. $db = mysqli_init();
  3. $db->real_connect('localhost', 'lorui', 'lorui.com', 'lorui_db ');
  4. /* SQL injection example*/
  5. $_POST['username'] = chr(0xbf) . chr(0×27) . ' OR username = username /*'; $_POST['password'] = ' guess'; $mysql['username'] = addslashes($_POST['username']); $mysql['password'] = addslashes($_POST['password']); $sql = "SELECT * FROM users WHERE username = '{$mysql['username']}' AND password = '{$mysql['password']}'"; $result = $db->query($sql); if ($result-> num_rows) { /* Success*/ } else { /* Failure*/ }
Copy code

Despite using addslashes, I can successfully log in without knowing the username and password. This vulnerability can be easily exploited for SQL injection. To avoid this vulnerability, use mysql_real_escape_string, prepared statements (Prepared Statements, or "parameterized queries"), or any of the mainstream database abstraction libraries.



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