mySql Injection Protect ?? Possible?

Discussion in 'PHP' started by fdoze, Mar 3, 2009.

  1. #1
    Hi,

    how can I protect my php web forms from mySQL injection? How can I make it hacker safe?


    Thanks in advance.
     
    fdoze, Mar 3, 2009 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    mysql_real_escape_string

    and ofcource you need to validate your inputs (try searching google for mysql injection prevention or something) and you'll find alot of resources!
     
    EricBruggema, Mar 3, 2009 IP
  3. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yep. Let nothing go from the user to the database without your code positively affirming that it follows the expected format.
     
    SmallPotatoes, Mar 3, 2009 IP
  4. fdoze

    fdoze Peon

    Messages:
    205
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    follows the expected format ? What do you mean?


    thanks.
     
    fdoze, Mar 3, 2009 IP
  5. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you are sending an integer in the query, make sure it is really an integer by processing it through intval() before it gets anywhere near the database. If you are sending a string, make sure it only contains the characters that are appropriate for your context. And so on.
     
    SmallPotatoes, Mar 3, 2009 IP
  6. belici

    belici Guest

    Messages:
    95
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I Give You Some Examples To Help You

    Let's Say That You Have getdata.php Which Takes POSTs From Another URLs

    You Have The Variable:
    
    $data = $_POST['data']; // gets the value that was posted from the field 'data'
    
    PHP:
    If You Are Already Connected To mySQL (User => Database) Then Use:
    
    $data = mysql_real_escape_string($_POST['data']); // this secures your value of 'data' by removing some forbidden characters & other stuff
    ...QUERY HERE...
    
    PHP:
    Also, If You Are Not Connected To The Database (And You Connect Only After Storing The Variable Value) Then Use:
    
    $data = htmlentities($_POST['data']); // this is not so secure like _real_escape_string but it does his job
    ...QUERY HERE...
    
    PHP:
     
    belici, Mar 3, 2009 IP
  7. aljosabre

    aljosabre Peon

    Messages:
    45
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    And watch out for other vulns .. like lfi, rfi .. they are most common just after sql injection.
     
    aljosabre, Mar 3, 2009 IP
  8. belici

    belici Guest

    Messages:
    95
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    yes but the most of the sites don't have it

    unless you have code's like:
    
    $IncludedFileName = $_GET['page'];
    
    if($IncludedFileName != NULL){
    include($IncludedFileName.php);
    }
    
    PHP:
    This Is For Local File Inclusion

    Anyhow, Watch Out How You Code Your Website!
     
    belici, Mar 3, 2009 IP
  9. fdoze

    fdoze Peon

    Messages:
    205
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    So, may I use mysql_real_escape_string($_REQUEST); ??? That should do the trick??

    thanks in advance.
     
    fdoze, Mar 3, 2009 IP
  10. belici

    belici Guest

    Messages:
    95
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    mysql_real_escape_string($_REQUEST);

    i don't think that it will work. you need the field name too... like

    mysql_real_escape_string($_REQUEST['data']); ( in case of POST ) ... in case of GET you put the GET data
     
    belici, Mar 3, 2009 IP
  11. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #11
    Use mysql_real_escape_string when you insert a value into database.

    Before that, verify the variables.

    EG: if the value should be a number, make sure it is.

    - ads2help
     
    ads2help, Mar 3, 2009 IP