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
Release: 2016-07-25 08:53:23
Original
937 people have browsed it
  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.



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template