PHP Security filtering code (360 high security factor)

Source: Internet
Author: User
PHP Security filtering code (360 high security factor)

  1. /**

  2. * Filter dangerous parameters
  3. * Edit: bbs.it-home.org
  4. */
  5. // Code By Safe3
  6. Function customError ($ errno, $ errstr, $ errfile, $ errline)
  7. {
  8. Echo"Error number:[$ Errno], error on line $ errline in $ errfile
    ";
  9. Die ();
  10. }
  11. Set_error_handler ("customError", E_ERROR );
  12. $ 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 )";
  13. $ 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 )";
  14. $ 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 )";
  15. Function StopAttack ($ StrFiltKey, $ StrFiltValue, $ ArrFiltReq ){

  16. If (is_array ($ StrFiltValue ))

  17. {
  18. $ StrFiltValue = implode ($ StrFiltValue );
  19. }
  20. If (preg_match ("/". $ ArrFiltReq. "/is", $ StrFiltValue) = 1 ){
  21. // Slog ("

    Operation IP address: ". $ _ SERVER [" REMOTE_ADDR "]."
    Operation Time: ". strftime (" % Y-% m-% d % H: % M: % S ")."
    Operation page: ". $ _ SERVER [" PHP_SELF "]."
    Submission method: ". $ _ SERVER [" REQUEST_METHOD "]."
    Parameter submitted: ". $ StrFiltKey ."
    Submit data: ". $ StrFiltValue );
  22. Print "360 websec notice: Illegal operation! ";
  23. Exit ();
  24. }
  25. }
  26. // $ ArrPGC = array_merge ($ _ GET, $ _ POST, $ _ COOKIE );
  27. Foreach ($ _ GET as $ key => $ value ){
  28. StopAttack ($ key, $ value, $ getfilter );
  29. } // Bbs.it-home.org
  30. Foreach ($ _ POST as $ key => $ value ){
  31. StopAttack ($ key, $ value, $ postfilter );
  32. }
  33. Foreach ($ _ COOKIE as $ key => $ value ){
  34. StopAttack ($ key, $ value, $ cookiefilter );
  35. }
  36. If (file_exists ('update360. php ')){
  37. Echo "please rename the file update360.php to prevent hackers from using
    ";
  38. Die ();
  39. }
  40. Function slog ($ logs)
  41. {
  42. $ Toppath = $ _ SERVER ["DOCUMENT_ROOT"]. "/log.htm ";
  43. $ Ts = fopen ($ toppath, "a + ");
  44. Fputs ($ Ts, $ logs. "\ r \ n ");
  45. Fclose ($ Ts );
  46. }
  47. ?>

Articles you may be interested in:

PHP filters post and get sensitive data instance codes. php filters illegal and special strings. php protects against injection of a piece of code (filter parameters) php regular-expression-based filtering of html tags, spaces, line breaks, and other code examples php provides a very useful method for filtering IP blacklist and whitelist. php prevents SQL injection vulnerability filtering function code php uses the filter function input. verify php to prevent SQL injection and regular expression filtering: a php filter of dangerous html code

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.