Prevent MySQL Injection

Discussion in 'PHP' started by webdude12, Mar 30, 2007.

  1. #1
    What is the best method to insure mysql injection doesnt happen as a hack, also if you use page.php?content=$id. How do you prevent people from entering in random ids?
     
    webdude12, Mar 30, 2007 IP
  2. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Use POST instead of GET.
     
    dp-user-1, Mar 30, 2007 IP
  3. VishalVasani

    VishalVasani Peon

    Messages:
    560
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hello,

    You can encrypt query string parameter so that no one know what parameter you are passing in query string....
     
    VishalVasani, Mar 31, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    That doesn't prevent anything.

    Make sure the variables you enter in your query string only contain characters that are needed for each field. Use intval() on variables that should ONLY be numeric, and mysql_real_escape_string() on all others. Additionally you can use preg_replace() to filter out unwanted characters...
     
    nico_swd, Mar 31, 2007 IP
  5. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #5
    That was in response to this:
    If I'm incorrect on this issue, I apologize. :)
     
    dp-user-1, Mar 31, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Yes, but malicious people can still submit any data they want via a HTML form. Plus, according to the filename and variable being passed, it seems like he has a PHP navigation which shows different pages based on the variable. So I don't think using POST variables would really be a good solution here.

    Sure, using POST is generally safer, I think. But I does not prevent MySQL injections, and isn't really appropriate here in my opinion. :)
     
    nico_swd, Mar 31, 2007 IP
  7. webdude12

    webdude12 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Well here is what I have so far....

    1. When possible I am trying to use non-standard spelling to variable names and table names i.e. instead of usname for username.
    2. Variables do not exactly match mysql field names
    3. Pages are only accessable when logged in, and I am checking for an nonstandard php session variable before runing any query.
    4. Inserts / updates are done with a session variable added to all tables (to track who inserted the data).

    Is there anything else I can do. My main concern is there is a semi-private content that can be displayed once logged in, and by randomingly guesinning the $id someone besides the user could intercept and change this data....
     
    webdude12, Mar 31, 2007 IP
  8. Karl Evans

    Karl Evans Peon

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I always just use mysql_real_escape_string().

    Does the job for me.
     
    Karl Evans, Mar 31, 2007 IP
  9. metallic07039

    metallic07039 Peon

    Messages:
    272
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Perhaps this function might help:

    Function Make_Safe($value) {
    return mysql_real_escape_string($value);
    }

    Put all of your variables that your passing into your SQL statement into this function. For example:

    $MyID = Make_Safe($MyID);
     
    metallic07039, Apr 1, 2007 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    And why not directly using mysql_real_escape_string() ? lol
     
    nico_swd, Apr 1, 2007 IP
  11. metallic07039

    metallic07039 Peon

    Messages:
    272
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Because you can add other provisions into that function if you needed. mysql_real_escape_string() is not the only one you should use.
     
    metallic07039, Apr 1, 2007 IP
  12. manilodisan

    manilodisan Peon

    Messages:
    224
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #12
    1. Always clean your content/id/paramater.
    2. Perform a check to see if the requested info exists
    3. if it exists, display results...

    function clean_content($content) {
      $content = stripslashes(trim($content));
      $content = nl2br($content);
      $content = htmlentities($content);
      return $content;
    }
    
    
    $checkData = mysql_query("SELECT * FROM ".$table." WHERE ".clean_content($parameter)." = ....");
    if (mysql_num_rows($checkData)!=0)
    {
         //display data
    }
    else {
        //throw an error
    }
    PHP:
     
    manilodisan, Apr 1, 2007 IP