Php is a simple method to prevent SQL injection and php SQL injection.

Source: Internet
Author: User

Php is a simple method to prevent SQL injection and php SQL injection.

This example describes how to implement SQL Injection Protection in php. We will share this with you for your reference. The details are as follows:

There is not much filtering here, mainly for the combination of php and mysql.

For general anti-injection, you only need to use the php addslashes function.

The following is a copy of the Code:

PHP code:

$_POST = sql_injection($_POST);$_GET = sql_injection($_GET);function sql_injection($content){if (!get_magic_quotes_gpc()) {if (is_array($content)) {foreach ($content as $key=>$value) {$content[$key] = addslashes($value);}} else {addslashes($content);}}return $content;}

For the system, you can use the following code and copy it.

PHP code:

Function inject_check ($ SQL _str) {return eregi ('select | insert | update | delete | \ '| \/\ * | \. \. \/| \. \/| union | into | load_file | outfile ', $ SQL _str); // filter} function verify_id ($ id = null) {if (! $ Id) {exit ('no submission parameter! ');} // Determines whether the elseif (inject_check ($ id) {exit ('the submitted parameter is invalid! ');} // Elseif (! Is_numeric ($ id) {exit ('the submitted parameter is invalid! ');} // Numeric judgment $ id = intval ($ id); // return $ id;} function str_check ($ str) {if (! Get_magic_quotes_gpc () {// judge whether magic_quotes_gpc is enabled $ str = addslashes ($ str); // filter} $ str = str_replace ("_","\_", $ str); // filter '_' out $ str = str_replace ("%", "\ %", $ str ); // filter '%' out return $ str;} function post_check ($ post) {if (! Get_magic_quotes_gpc () {// judge whether magic_quotes_gpc is enabled $ post = addslashes ($ post ); // filter submitted data when magic_quotes_gpc is not enabled} $ post = str_replace ("_", "\ _", $ post ); // filter '_' out $ post = str_replace ("%", "\ %", $ post ); // filter '%' out $ post = nl2br ($ post); // press enter to convert $ post = htmlspecialchars ($ post); // convert the html tag to return $ post ;}

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.