Jump to content

mysql_real_escape_string vs my code


runnerjp

Recommended Posts

It has no advantages OVER mysql_real_escape_string() but it does help, depending on what you're trying to achieve.

 

mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a.

 

mysql_real_escape_string() prevents SQL-Injections, however, things such as htmlspecialchars() can help prevent Cross-site scripting.

 

Do you see the difference?

so if i did this

 

function check_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    $data = mysql_real_escape_string($data) ;
    return $data;
}

 

would that work to prevent both sql and cross site??

 

mysql_real_escape_string()

Why do you feel the need to call trim, stripslashes and htmlspecialchars on the input?  You seem to be confusing a couple of things, or trying to do everything in one place.  In particular, htmlspecialchars has no place being anywhere near input... why would anyone want to store all input data as HTML-encoded strings? Sure, it can be useful when outputting data (in this case, from the database).

 

Back to the original question, the only advantage would be towards an attacker looking for an easy way into your site.  :(

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.