Home  >  Article  >  Backend Development  >  PHP prevents sql injection function (discuz)

PHP prevents sql injection function (discuz)

WBOY
WBOYOriginal
2016-07-25 08:53:161032browse
  1. $magic_quotes_gpc = get_magic_quotes_gpc();

  2. @extract(daddslashes($_COOKIE));
  3. @extract(daddslashes($_POST));
  4. @extract(daddslashes($_GET));
  5. if(!$magic_quotes_gpc) {
  6. $_FILES = daddslashes($_FILES);
  7. }

  8. function daddslashes($string, $force = 0) {

  9. if(!$GLOBALS[' magic_quotes_gpc'] || $force) {
  10. if(is_array($string)) {
  11. foreach($string as $key => $val) {
  12. $string[$key] = daddslashes($val, $force) ;
  13. }
  14. } else {
  15. $string = addslashes($string);
  16. }
  17. }
  18. return $string;
  19. }

Copy code

You can enhance the code below to protect it For server security, it is very important for PHP to prevent SQL injection security functions!

  1. /*

  2. Function name: inject_check()
  3. Function function: Detect whether the submitted value contains SQL injection characters, prevent injection, and protect server security
  4. Parameters: $sql_str: Submitted Variable bbs.it-home.org
  5. Return value: Return detection result, true or false
  6. */
  7. function inject_check($sql_str) {
  8. return eregi('select|insert|and|or|update|delete|'|/ *|*|../|./|union|into|load_file|outfile', $sql_str); // Filter
  9. }

  10. /*

  11. Function name: verify_id()
  12. Function: Verify whether the submitted ID value is legal
  13. Parameters: $id: Submitted ID value
  14. Return value: Return the processed ID
  15. */
  16. function verify_id($id=null) {
  17. if (!$id ) { exit('No parameters submitted!'); } // Judgment of whether it is empty
  18. elseif (inject_check($id)) { exit('The parameters submitted are illegal!'); } // Judgment of injection
  19. elseif (!is_numeric ($id)) { exit('The submitted parameter is illegal!'); } // Numeric judgment
  20. $id = intval($id); // Integerization

  21. return $id ;

  22. }

  23. /*

  24. Function name: str_check()
  25. Function function: filter the submitted string
  26. Parameters: $var: the string to be processed
  27. Return value: return filtering The string after
  28. */
  29. function str_check( $str ) {
  30. if (!get_magic_quotes_gpc()) { // Determine whether magic_quotes_gpc is turned on
  31. $str = addslashes($str); // Filter
  32. }
  33. $str = str_replace("_", "_", $str); // Filter out '_'
  34. $str = str_replace("%", "%", $str); // Filter out '%'< /p>
  35. return $str;

  36. }

  37. /*

  38. Function name: post_check()
  39. Function function: Process the submitted editing content
  40. Parameters: $post: Content to be submitted
  41. Return value: $post: Return filtered content
  42. */
  43. function post_check($post) {
  44. if (!get_magic_quotes_gpc()) { // Determine whether magic_quotes_gpc is open
  45. $post = addslashes($ post); // Filter the submitted data when magic_quotes_gpc is not turned on
  46. }
  47. $post = str_replace("_", "_", $post); // Filter out '_'
  48. $post = str_replace( "%", "%", $post); // Filter out '%'
  49. $post = nl2br($post); // Enter conversion
  50. $post = htmlspecialchars($post); // HTML tag conversion

  51. Put it in a common call file (such as the conn database link file) and filter all GET or POST data with special strings to achieve simple and effective SQL injection filtering. Code:
Function inject_check($sql_str) {

return eregi('select|insert|and|or|update|delete|'|/*|*|../|./|union |into|load_file|outfile', $sql_str);

}
if (inject_check($_SERVER['QUERY_STRING'])==1 or inject_check(file_get_contents("php://input"))==1){
//echo "Warning illegal access!";
    header("Location: Error.php");
  1. }
  2. Copy code
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn