PHP Web page to prevent SQL injection method Configuration _php Tutorial

Source: Internet
Author: User
Prerequisite is that we need to have the server's administrative rights, that is, you can modify the php.ini file, the following I would like to introduce the modification of PHP configuration file to prevent SQL injection method has to learn the friend can refer to.

To be safe, you can open the Safe mode of the "php.ini" file, set "Safe_mode=on", and the "display_erros" option that displays the PHP execution error message will return a lot of available information to the intruder if it is turned on, so set it to " Display_erros=off "so that the information after the PHP function execution error will not be displayed in the client's browser.

In addition, there is a very important configuration option in the file, if the "MAGIC_QUOTES_GPC" item is set to "on", the PHP program automatically converts the user-submitted variables to include "'", "" "," "and" "automatically into the escape character with backslash. This option is similar to the parameter filtering in ASP programs, which can be used to prevent most character injection attacks.

A program is pretty good if you don't have server admin rights

The code is as follows Copy Code

Class Sqlsafe {
Private $getfilter = "' | (And|or) \b.+? (>|<|=|in|like) |\/\*.+?\*\/|<\s*script\b|\bexec\b| Union.+? Select| Update.+? Set| Insert\s+into.+? values| (select| DELETE). +? From| (create| alter| drop| TRUNCATE) \s+ (table| DATABASE) ";
Private $postfilter = "\b (and|or) \b.{1,6}?" (=|>|<|\bin\b|\blike\b) |\/\*.+?\*\/|<\s*script\b|\bexec\b| Union.+? Select| Update.+? Set| Insert\s+into.+? values| (select| DELETE). +? From| (create| alter| drop| TRUNCATE) \s+ (table| DATABASE) ";
Private $cookiefilter = "\b (and|or) \b.{1,6}?" (=|>|<|\bin\b|\blike\b) |\/\*.+?\*\/|<\s*script\b|\bexec\b| Union.+? Select| Update.+? Set| Insert\s+into.+? values| (select| DELETE). +? From| (create| alter| drop| TRUNCATE) \s+ (table| DATABASE) ";
/**
* Constructor function
*/
Public Function __construct () {
foreach ($_get as $key = $value) {$this->stopattack ($key, $value, $this->getfilter);}
foreach ($_post as $key = $value) {$this->stopattack ($key, $value, $this->postfilter);}
foreach ($_cookie as $key = $value) {$this->stopattack ($key, $value, $this->cookiefilter);}
}
/**
* parameter check and write log
*/
Public Function Stopattack ($StrFiltKey, $StrFiltValue, $ArrFiltReq) {
if (Is_array ($StrFiltValue)) $StrFiltValue = implode ($StrFiltValue);
if (Preg_match ("/". $ArrFiltReq. " /is ", $StrFiltValue) = = 1) {
$this->writeslog ($_server["REMOTE_ADDR"]. " ". Strftime ("%y-%m-%d%h:%m:%s ")." ". $_server[" Php_self "]." ". $_server[" Request_method "]." ". $StrFiltKey." ". $StrFiltValue);
ShowMsg (' You submitted the parameter is illegal, the system has recorded your this operation! ', ', 0, 1);
}
}
/**
* SQL injection Log
*/
Public Function Writeslog ($log) {
$log _path = cache_path. ' Logs '. Directory_separator. ' Sql_log.txt ';
$ts = fopen ($log _path, "A +");
Fputs ($ts, $log. " RN ");
Fclose ($ts);
}
}
?>

http://www.bkjia.com/PHPjc/629633.html www.bkjia.com true http://www.bkjia.com/PHPjc/629633.html techarticle prerequisite is that we need to have the server's administrative rights, that is, you can modify the php.ini file, the following I would like to introduce the modified PHP configuration file to prevent SQL injection method has to learn the friend can ...

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