Jump to content

[SOLVED] mysql_real_escape_string


phpfan

Recommended Posts

Hi everybody,

 

I have try to use mysql_real_escape_string in order to prevent any sql injection attacks but i am getting some unexpected results. When i test the variable which is holding the string value, before i apply the above function i get the following result \'Tom\' (the string tha i pass was 'Tom'). After that i apply the mysql_real_escape_string i get the following result \\\'tom\\\'. I was expecting to get the first result after the application of mysql_real_escape_string function. Can you please explain me why i am getting these results...  ???

 

I am using PHP 5.2.1 and MySQL client version: 5.0.33

 

Link to comment
Share on other sites

You most probably have a setting called magic_quotes_gpc enabled which is why you get \'Tom\' before applying mysql_real_escape_string.

 

You're best of checking the status of magic_quotes before using anyescape functions. SO create a function like this:

 

function makeSQLSafe($str)
{
    // check the status of magic_quotes_gpc, if it this returns true 
    // we remove the escaped characters. Allowing for the real escaping 
    // to be done via mysql_real_escape_string
    if(get_magic_quotes_gpc())
    {
        // remove the slashes.
        $str = stripslashes($str);
    }

    $str = mysql_real_escape_string($str);

    return $str;
}

// example usage:
$username = makeSQLSafe($_POST['username']);

Link to comment
Share on other sites

the mysql_real_escape_string function escapes any characters within a string to prevent SQL injection attacks. Its is recommended to use this function when dealing with any data that is being inserted into a database. Do not use raw variables in SQL queries example this is very bad:

 

$sql = "SELECT * FROM users WHERE username='$_POST['username']' AND password='$_POST['password']'";

 

As that query will allow for SQL Injection attacks. For example a malicous attacker may insert special SQL code in to the username field like the following:

' OR 1=1 --

 

What that will do is change the query to this:

SELECT * FROM users WHERE username='' OR 1=1 -- ' AND password=''

 

That now totally changes the query sent to MySQL. It will tell MYSQL to select the row from the users table where the username is blank OR if 1=1. No 1 always equals to 1 so MySQL will fetch the first row in the users table. This row will more than likely be the one that contains the administrators username and password . So now the malicious attacker is now logged into your web site as the administrator!

 

However if you did this:

$user = mysql_real_escape_string($_POST['username']);
$pass = mysql_real_escape_string($_POST['password']);

$sql = "SELECT * FROM users WHERE username='$user' AND password='$pass'";

 

And you entered ' OR 1=1 -- into the username field. The query now becomes:

SELECT * FROM users WHERE username='\' OR 1=1 --' AND password=''

 

Which tells MySQL to select the row from the users that where the username matchs ' OR 1=1 -- and the password is blank. That is now much more safer.

 

To learn more about SQL Injection do a google search and you will get lots of articles/tutorials that explain what it means and how to prevent it.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.