Difference between the PHP function addslashes and mysql_real_escape_string

Source: Internet
Author: User
Tags sql injection example
Difference between the PHP function addslashes and mysql_real_escape_string

  1. Create table users (
  2. Username VARCHAR (32) character set gbk,
  3. Password VARCHAR (32) character set gbk,
  4. Primary key (username)
  5. );

In this example, when only addslashes (or magic_quotes_gpc) is used to escape the queried data:

  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) {/* succeeded */} else {/* failed */}

Even if addslashes is used, you can log on successfully without knowing the user name and password. This vulnerability can be easily exploited for SQL injection. To avoid this vulnerability, use mysql_real_escape_string, the Prepared statement (Prepared Statements, I .e. "parameterized query"), or any mainstream database abstract class library.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.