Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: secure mysql usage

  1. #1
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default secure mysql usage

    Hello guys,
    I have a couple of quick questions regarding mysql queries.
    Right know I check the database string values that people insert from the site like this:
    PHP Code:
    if (get_magic_quotes_gpc()) {
        return 
    stripslashes($input);
    } else {
        return 
    mysqli_real_escape_string($db,$input);

    This is just a guess but won't I need to add mysqli_real_escape_string after stripslashes if I got magic quotes on and will mysqli_real_escape_string be enough to secure the string?

    And will I also have to check the inputs with mysqli_real_escape_string once people want to load data (besides for inserting data) and is there a way I can check if the data is a number instead of string?

    thanks in advance

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    You should be using mysqli_real_escape_string, and htmlentities just to be safe.

    Here's to check if it's an integer:
    PHP Code:
    <?php
    if(!is_int($string)){
      echo 
    "This is the output if \$string is not a number.";
    } else {
      echo 
    "This is the output if \$string is a number.";
    }
    ?>
    To see if it is numeric, change the is_int to is_numeric.
    Jeremy | jfein.net

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Learn to use PDO. Prepared statements and bound variables are a blessing — security is pretty much taken care of automatically.

    You're quite right, stripping the slashes makes it, if anything, more important to escape the values. The code should look like this:
    Code:
    return mysqli_real_escape_string(get_magic_quotes_gpc() ? stripslashes($input) : $input);
    A neater solution, though, might be to use a wrapper function around GET/POST/COOKIE values in the first place:
    Code:
    function g($k) {
      return get_magic_quotes_gpc() ? stripslashes($_GET[$k]) : $_GET[$k];
    }
    Alternatively, simply disable magic quotes in the first place.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the info guys, this is really useful!

  5. #5
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Glad to help you Dennis.
    Jeremy | jfein.net

  6. #6
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    just a little side question, when I use htmlentities to clean out the html tags and special characters and then load it with ajax I will see the cleaned code instead of the "converted", example:

    I insert "hello & welcome" and use htmlentities on it, I will then output:
    "hello &amp; welcome" once I load it (with ajax that is).. is there a way to bypass this?

  7. #7
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    If you're seeing those then you don't want to be using htmlentities(). htmlentities() is only for data that's going to be inserted into an HTML page; if it isn't going to be inserted then you don't need to do it. Additionally, ensure that the operation is only performed once on any input.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Do I have to replace all the special characters again?
    Won't that break the xml structure again? I could of course replace them inside javascript if thats necessary, was just hoping for a simple feature to do this backwards.

  10. #10
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    I think you would do something like this:
    PHP Code:
    <?php
    $content 
    "A &amp; B";
    $content str_replace('&amp;','&',$content);
    echo 
    $content;
    ?>
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •