Python Dafa good--mysql anti-injection

Source: Internet
Author: User
Tags how to prevent sql injection

MySQL and SQL Injection

SQL injection security can occur if you get user-entered data from a Web page and insert it into a MySQL database.

This section will show you how to prevent SQL injection and use scripts to filter the characters injected into SQL.

SQL injection, by inserting a SQL command into a Web form to submit or entering a query string for a domain name or page request, eventually achieves a malicious SQL command that deceives the server.

We should never trust the user input, we must assume that the user input data is not safe, we all need to the user input data to filter processing.

In the following instance, the user name entered must be a combination of letters, numbers, and underscores, with a user name length between 8 and 20 characters:

if (Preg_match ("/^\w{8,20}$/", $_get['username'), $ Matches) {   "                          WHERE username= $matches [0]");  Else  {   "username input exception ";}

Let's look at the SQL that appears when no special characters are filtered:

 "Qadir"; DELETE from users; "  "select * from users WHERE name= ' {$name} '");

In the above injection statement, we did not filter the $name variables, we inserted the SQL statements we did not need, and all the data in the users table would be deleted.

Mysqli_query () in PHP is not allowed to execute multiple SQL statements, but SQLite and PostgreSQL can execute multiple SQL statements at the same time, so we need to rigorously validate the data of these users.

To prevent SQL injection, here are a few important points to note:

    • 1. Never trust the user's input. The user's input can be verified by regular expressions, or by limiting the length, by converting the single quotation mark and the double "-".
    • 2. Never use dynamically assembled SQL, either using parameterized SQL or directly using stored procedures for data query access.
    • 3. Never use a database connection with administrator rights, and use a separate limited database connection for each app.
    • 4. Do not store confidential information directly, encrypt or hash out passwords and sensitive information.
    • 5. Applied exception information should give as few hints as possible, preferably using a custom error message to wrap the original error message
    • 6.sql injection detection method generally take the aid software or website platform to detect, software generally use SQL injection detection Tool Jsky, website platform has billion think website security platform detection tools. Mdcsoft scan and so on. The use of mdcsoft-ips can effectively protect against SQL injection, XSS attacks and so on.
Prevent SQL injection

In scripting languages, such as Perl and PHP, you can escape the data entered by the user to prevent SQL injection.

PHP's MySQL extension provides the mysqli_real_escape_string () function to escape special input characters.

if (GET_MAGIC_QUOTES_GPC ()) {  =="select * from users WHERE name= ' {$name} ' ");
Injection in a LIKE statement

Like query, if the user entered the value of "_" and "%", then this situation will occur: the user is only want to query "Abcd_", query results are "abcd_", "ABCDE", "ABCDF" and so on, the user to query "30%" (note: 30%) When the problem occurs.

In PHP scripts we can use the addcslashes () function to handle this situation, as in the following example:

= addcslashes(mysqli_real_escape_string($conn,"%something_"),"%_");  $sub = = \%something\_ mysqli_query($conn,"SELECT * from messages WHERE subject like ' {$sub}% ' ");

The Addcslashes () function adds a backslash before the specified character.

Syntax format:


Python Dafa good--mysql anti-injection

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: 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.