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 ); } } ?> |
...