0

I have make a simple function for security prevent from sql injection and XXS here is my code, any suggestion for this? Is this good enough for security?

function mres($input){
    if(get_magic_quotes_gpc()){
        $input=stripslashes($input);    
    }
    $input=htmlentities($input, ENT_COMPAT, 'UTF-8');
    return mysql_real_escape_string($input);
}
chien pin wang
  • 559
  • 1
  • 4
  • 15

2 Answers2

2

This is wrong in at least two ways:

  1. Turn of magic_quotes completely if you can. At least you are not using it, but $input may not be scalar
  2. htmlentities is for display, not storage. Never encode for storage!
  3. mysql_* functions are deprecated. There is no guarantee you will have an open mysql connection (required) when you call it either.

http://us3.php.net/manual/en/function.mysql-real-escape-string.php

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

Look what you're actually doing:

Magic quotes is a bulk escaping of all incoming data, which makes you vulnerable, as escaping alone doesn't make your data "safe" by any means.

So, you are cleaning these bulk escapes... and then apply the very same escaping again :)

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Not exactly .. `mysql_real_escape_string` and `addslashes` are not equivalent. Plus `htmlentities` encodes quotes that would be escaped. – Explosion Pills Jan 24 '13 at 14:02