which the best method to prevent sql injection?

Discussion in 'PHP' started by ankifreeze, Jan 25, 2011.

  1. #1
    hello,


    I want to sanitize & validate my input to prevent injection . can you give me suggestion which method better to sanitize & validate email,username,number input?

    username

    if(!filter_has_var(INPUT_POST, "username"))
      {
      echo("Input type does not exist");
      }
    else
      {
      $url = filter_input(INPUT_POST,
      "username",FILTER_SANITIZE_STRING);
      }
    PHP:
    OR
    $username = mysql_real_escape_string($_POST['username']);
    {if (!empty($username)) {
    $username = TRUE
    } else {
    $username = FALSE;
    echo "<p><font color='red'>Please enter your username!</font></p>";
    }
    
    
    PHP:

    Number
     $number=abs((int)$_GET['number']);
    
    PHP:
    OR

    if(!filter_has_var(INPUT_POST, "number"))
      {
      echo("Input type does not exist");
      }
    else
      {
      $url = filter_input(INPUT_POST,
      "number", FILTER_VALIDATE_INT);
      }
    PHP:

    email

    
    $email=mysql_real_escape_string($_POST['email'];
    if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email)) {
        echo "email is valid";
    }
    else {echo"
    email is not valid";
    
    
    
    PHP:
    OR
    function spamcheck($field)
      {
      
      $field=filter_var($field, FILTER_SANITIZE_EMAIL);
    
      if(filter_var($field, FILTER_VALIDATE_EMAIL))
        {
        return TRUE;
        }
      else
        {
        return FALSE;
        }
      }
    
    if (isset($_POST['email']))
      {
      $mail = spamcheck($_POST['email']);
      if ($mail==FALSE)
        {
        echo "Invalid input";
        }else{echo "valid input";}
    
    
    
    PHP:

     
    ankifreeze, Jan 25, 2011 IP
  2. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #2
    Using mysql_real_escape_string on user data should prevent SQL injection in most cases.
     
    Alex Roxon, Jan 25, 2011 IP
  3. ankifreeze

    ankifreeze Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    yes...but not in case to sanitize & validate email & number.....
     
    ankifreeze, Jan 25, 2011 IP
  4. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #4
    why email and number? In case of email, deletee that mysql_real_escape_string and place it AFTER checking with preg_match
     
    G3n3s!s, Jan 25, 2011 IP
  5. ankifreeze

    ankifreeze Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    maybe I wrong about email but about number abs((integer)) is most effective in use....it converts anything input into integer...negatif number into positif and convert alphabet & another var into 0...
     
    ankifreeze, Jan 25, 2011 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    Basically validate data (using preg_match(), is_numeric() etc.)...then sanitize data (mysql_real_escape_string()).
     
    danx10, Jan 26, 2011 IP
  7. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #7
    I think number can't be "hacked" when it's INT ... just use function danx mentioned (mysql_real_escape_string()).
     
    G3n3s!s, Jan 27, 2011 IP
  8. drctaccess

    drctaccess Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #8
    drctaccess, Jan 28, 2011 IP
  9. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #9
    drctaccess - never heard about this option, thank you :)
     
    G3n3s!s, Jan 28, 2011 IP