Common prevention methods for mysql injection by php

Source: Internet
Author: User

Now I will summarize the following main types of code: for integer data, use intval to forcibly convert mysql to prevent injection before insertion. Generally, it is to submit (post or get) pages) the following two types of data are received: one is the php logic module. To escape special characters submitted on the page, use get_magic_quotes_gpc () to check whether automatic escape settings are enabled. If this setting is not enabled, you can use the addslashes () function to add a backslash to the database query statement and so on. For example:

If (! Get_magic_quotes_gpc () {foreach ($ _ POST as $ _ key =>$ _ value) {if (! Is_array ($ _ value) $ _ POST [$ _ key] = addslashes ($ _ value );}}

 

The second is the mysql processing module. Using the mysql_real_escape_string () function of mysql, you can escape special characters and characters that may cause database operation errors. For example:
If (get_magic_quotes_gpc () {$ str = stripslashes ($ str);} else {$ str = mysql_real_escape_string ($ str );}

 

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.