Several methods to prevent SQL injection in php

Source: Internet
Author: User
Tags sql injection prevention
Several methods to prevent SQL injection in php

  1. /**
  2. * SQL Injection Prevention
  3. * @ Author: test@jbxue.com
  4. **/
  5. /**
  6. * Reject SQL inject
  7. */
  8. If (! Function_exists (quote ))
  9. {
  10. Function quote ($ var)
  11. {
  12. If (strlen ($ var ))
  13. {
  14. $ Var =! Get_magic_quotes_gpc ()? $ Var: stripslashes ($ var );
  15. $ Var = str_replace ("'", "\'", $ var );
  16. }
  17. Return "'$ var '";
  18. }
  19. }
  20. If (! Function_exists (hash_num )){
  21. Function hash_num ($ input)
  22. {
  23. $ Hash = 5381;
  24. For ($ I = 0; $ I <strlen ($ str); $ I ++)
  25. {
  26. $ C = ord ($ str {$ I });
  27. $ Hash = ($ hash <5) + $ hash) + $ c;
  28. }
  29. Return $ hash;
  30. }
  31. }
  32. ?>

Test:

  1. /**
  2. * Anti-SQL Test Code
  3. Create table if not exists 'TB '(
  4. 'Id' int (10) unsigned not null auto_increment,
  5. 'Age' tinyint (3) unsigned not null,
  6. 'Name' char (100) not null,
  7. 'Note' text not null,
  8. Primary key ('id ')
  9. ) ENGINE = MyISAM default charset = utf8;
  10. **/
  11. Include_once ('Common. php ');
  12. Var_dump (hash_num ('ddddd '));
  13. If (empty ($ _ GET ))
  14. {
  15. $ _ GET = array ('age' => '99', 'name' => 'A \ 'B \ 'C ";', 'note' => "A' B \ '\ nC #");
  16. }
  17. $ Age = (int) $ _ GET ['age'];
  18. $ Name = quote ($ _ GET ['name']);
  19. $ Note = quote ($ _ GET ['note']);
  20. $ SQL = "INSERT INTO 'TB' ('age', 'name', 'note') VALUES
  21. ($ Age, $ name, $ note )";
  22. Var_dump ($ SQL );
  23. ?>

# ------------------ Method 2:

  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. }
  20. ?>

Method 3:

  1. Function inject_check ($ SQL _str) {// prevents injection
  2. $ Check = eregi ('select | insert | update | delete | '|/* | .. /|. /| union | into | load_file | outfile ', $ SQL _str );
  3. If ($ check ){
  4. Echo "illegal injection content entered! ";
  5. Exit ();
  6. } Else {
  7. Return $ SQL _str;
  8. }
  9. }
  10. Function checkurl () {// check the origin path
  11. If (preg_replace ("/https tutorial? : // ([^:/] +). */I "," 1 ", $ _ server ['http _ referer'])! = Preg_replace ("/([^:] +). */", "1", $ _ server ['http _ host']) {
  12. Header ("location: http://s.jbxue.com ");
  13. Exit ();
  14. }
  15. }
  16. // Call
  17. Checkurl ();
  18. $ Str = $ _ get ['URL'];
  19. Inject_check ($ SQL _str); // you can perform this operation when obtaining the parameter.

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.