Php simple parameter filtering code learning

Source: Internet
Author: User
Php simple parameter filtering code learning

  1. /**
  2. * Parameter filtering code
  3. * Edit bbs.it-home.org
  4. */
  5. If (@ get_magic_quotes_gpc ()){
  6. $ _ GET = sec ($ _ GET );
  7. $ _ POST = sec ($ _ POST );
  8. $ _ COOKIE = sec ($ _ COOKIE );
  9. $ _ FILES = sec ($ _ FILES );
  10. }
  11. $ _ SERVER = sec ($ _ SERVER );
  12. Function sec (& $ array ){
  13. // If it is an array, traverse the array and call it recursively
  14. If (is_array ($ array )){
  15. Foreach ($ array as $ k => $ v ){
  16. $ Array [$ k] = sec ($ v );
  17. }
  18. } Else if (is_string ($ array )){
  19. // Use the addslashes function for processing
  20. $ Array = addslashes ($ array );
  21. } Else if (is_numeric ($ array )){
  22. $ Array = intval ($ array );
  23. }
  24. Return $ array;
  25. }
  26. ?>

1. integer parameter determination when the input parameter YY is an integer, usually abc. the SQL statement in asp is roughly as follows: select * from table name where field = YY, so you can use the following steps to test whether SQL injection exists. ① HTTP: // xxx. xxx. xxx/abc. asp? P = YY '(append a single quotation mark). at this time, abc. the SQL statement in ASP is changed to select * from table name where field = YY ', abc. asp running exception; ② HTTP: // xxx. xxx. xxx/abc. asp? P = YY and 1 = 1, abc. asp is running normally, and it works properly with HTTP: // xxx. xxx. xxx/abc. asp? P = YY: The running result is the same; ③ HTTP: // xxx. xxx. xxx/abc. asp? P = YY and 1 = 2, abc. asp is abnormal. if the preceding three steps are fully met, the SQL injection vulnerability exists in abc. asp.

The code of an integer filter function is as follows:

  1. Function num_check ($ id ){

  2. If (! $ Id ){
  3. Die ('parameter cannot be blank! ');
  4. } // Whether it is null
  5. Else if (inject_check ($ id )){
  6. Die ('invalid parameter ');
  7. } // Injection judgment
  8. Else if (! Is_numetic ($ id )){
  9. Die ('invalid parameter ');
  10. }
  11. // Digital judgment
  12. $ Id = intval ($ id );
  13. // Integer
  14. Return $ id;
  15. }

  16. // Character filtering function

  17. Function str_check ($ str ){
  18. If (inject_check ($ str )){
  19. Die ('invalid parameter ');
  20. }
  21. // Injection judgment
  22. $ Str = htmlspecialchars ($ str );
  23. // Convert html
  24. Return $ str;
  25. }
  26. Function search_check ($ str ){
  27. $ Str = str_replace ("_", "_", $ str );
  28. // Filter out "_"
  29. $ Str = str_replace ("%", "%", $ str );
  30. // Filter out "%"
  31. $ Str = htmlspecialchars ($ str );
  32. // Convert html
  33. Return $ str;
  34. }
  35. // Form filter function
  36. Function post_check ($ str, $ min, $ max ){
  37. If (isset ($ min) & strlen ($ str) <$ min ){
  38. Die ('minimum $ min Byte ');
  39. } Else if (isset ($ max) & strlen ($ str)> $ max ){
  40. Die ('maximum $ max Byte ');
  41. }
  42. Return stripslashes_array ($ str );
  43. }
  44. ?>

When the input parameter YY is a string, it is usually abc. the SQL statement in asp is roughly as follows: select * from table name where field = 'yy', so you can test whether SQL injection exists by following these steps. ① HTTP: // xxx. xxx. xxx/abc. asp? P = YY '(append a single quotation mark). at this time, abc. the SQL statement in ASP is changed to select * from table name where field = YY ', abc. asp running exception; ② HTTP: // xxx. xxx. xxx/abc. asp? P = YY &; nb... 39; 1' = '1', abc. asp is running normally, and is consistent with HTTP: // xxx. xxx. xxx/abc. asp? P = YY: The running result is the same; ③ HTTP: // xxx. xxx. xxx/abc. asp? P = YY &; nb... 39; 1' = '2', abc. asp is abnormal. if the preceding three steps are fully met, the SQL injection vulnerability exists in abc. asp.

Attach a function to prevent SQL injection:

  1. // Anti-injection function
  2. Function inject_check ($ SQL _str ){
  3. Return eregi ('select | inert | update | delete | '|/* |.../|./| UNION | into | load_file | outfile', $ SQL _str );
  4. // Filter, prevent injection bbs.it-home.org
  5. }
  6. Function stripslashes_array (& $ array ){
  7. If (is_array ($ array )){
  8. Foreach ($ array as $ k => $ v ){
  9. $ Array [$ k] = stripslashes_array ($ v );
  10. }
  11. } Else if (is_string ($ array )){
  12. $ Array = stripslashes ($ array );
  13. }
  14. Return $ array;
  15. }
  16. ?>

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.