Copy codeThe Code is as follows:
<? Php
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. "\ r \ n ");
Fclose ($ ts );
}
}
?>