PHP web page prevention SQL injection method configuration _ PHP Tutorial

Source: Internet
Author: User
Tags sql injection methods
PHP web page protection SQL injection method configuration. The precondition is that we need to have the server management permission, that is, we can modify php. ini file. next I will introduce how to modify the php configuration file to prevent SQL injection methods. if you need to learn it, you must have the server management permission, that is, you can modify php. ini file. next I will introduce how to modify the php configuration file to prevent SQL injection methods. For more information, see.

For security, you can open "php. set "safe_mode = On" for the security mode of the ini file. if the "display_erros" option that displays the PHP execution error message is enabled, a lot of available information will be returned to the intruders, therefore, set it to "display_erros = off". In this way, the information after the PHP function execution error is not displayed in the browser of the client.

In addition, there is an important configuration option in the file. if you set the "magic_quotes_gpc" item to "On ", the PHP program automatically converts the variables submitted by the user into escape characters containing backslashes. This option is similar to parameter filtering in ASP, which can prevent most injection attacks.

A program is very good. if you do not have the server management permission

The code is as follows:

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
*/
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 );}
}
/**
* Check parameters and write logs
*/
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 ('The parameter you submitted is invalid. The system has recorded your operation! ', '', 0, 1 );
}
}
/**
* SQL injection logs
*/
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 );
}
}
?>

...

Related Article

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.