views:

3790

answers:

8

I just moved to a new hosting company and now whenever a string gets escaped using:

mysql_real_escape_string($str);

the slashes remain in the database. This is the first time I've ever seen this happen so none of my scripts use

stripslashes()

anymore.

This is on a CentOS 4.5 64bit running php 5.2.6 as fastcgi on a lighttpd 1.4 server. I've ensured that all magic_quotes options are off and the mysql client api is 5.0.51a.

I have the same issue on all 6 of my webservers.

Any help would be appreciated.

Thanks.

Edit:

Magic Quotes isn't on. Please don't recommend turning it off. THIS IS NOT THE ISSUE.

A: 

mysql_real_escape_string($str); is supposed to do exactly that. it is meant to add backslashes to special characters especially when you want to pass the query to mysql. Take note that it also takes into account the character set of mysql.

For safer coding practices it would be good to edit your code and use stripslashes() to read out the data and remove the slashes.

Steve Obbayi
Wouldn't running stripslashes() on a non-escaped string remove good slashes too?
Eric Lamb
The slashes are only to escape the string that is being inserted, they're not supposed to end up in the database. You should never have to run stripslashes() on data you pulled from the database.
MrZebra
slashes would also be escaped from \ to \\ when using mysql_real_escape_string($str); if you use stripslashes() in the same text then it would remove any extra slashes and you end up with 1 slash.
Steve Obbayi
+6  A: 

The host that you've moved probably has magic_quotes_runtime turned on. You can turn it off with set_magic_quotes_runtime(0).

Please turn off magic_quotes_runtime, and then change your code to use bind variables, rather than using the string escaping.

Andy Lester
Sorry man, but this didn't help at all. Thanks though.
Eric Lamb
It's still the only *right* answer.
Andrew Medico
How can it be right if it doesn't fix the issue? That doesn't make any sense...
Eric Lamb
The actual "right" part of it is the part, that suggests the use of bind variables instead of string concatenation/escaping, thus avoiding all the troubles of escaping, quoting, potential security holes (hello, sql injections) and so on
shylent
A: 

it sounds as though you have magic quotes turned on. Turning it off isn't too hard: just create a file in your root directory called .htaccess and put this line in it:

php_flag magic_quotes off

If that's not possible for whatever reason, or you want to change your application to be able to handle magic quotes, use this technique:

Instead of accessing the request variables directly, use a function instead. That function can then check if magic quotes is on or off and strip out slashes accordingly. Simply running stripslashes() over everything won't work, because you'll get rid of slashes which you actually want.

function getVar($key) {
    if (get_magic_quotes_gpc()) {
        return stripslashes($_POST[$key]);
    } else {
        return $_POST[$key];
    }
}

$x = getVar('x');

Now that you've got that, all your incoming variables are ready to be escaped again and mysql_real_escape_string() won't stuff them up.

nickf
Note that you may not always be able to disable magic_quotes in a .htaccess file. If the value is set with php_admin_flag in php.ini, it can't be overridden later.
troelskn
A: 

You must probably have magic quotes turned on. Figuring out exactly how to turn it off can be quite a headache in PHP. While you can turn off magic quotes with set_magic_quotes_runtime(0), it isn't enough -- Magic quotes has already altered the input data at this point, so you must undo the change. Try with this snippet: http://talks.php.net/show/php-best-practices/26

Or better yet -- Disable magic quotes in php.ini, and any .htaccess files it may be set in.

troelskn
A: 

Function below will correctly remove slashes before inserting into the database. I know you said magic quotes isn't on but something is adding slashes so try the following page and see the output. It'll help figure out where. Call with page.php?var=something-with'data_that;will`be|escaped

You will most likely see number three outputting more slashes than needed.

*Change the db details too.

<?php

$db = mysql_connect('host', 'user', 'pass');

$var = $_REQUEST['var'];
echo "1: $var :1<br />";
echo "2: ".stripslashes($var)." :2<br />";
echo "3: ".mysql_real_escape_string($var)." :3<br />";
echo "4: ".quote_smart($var)." :4<br />";


function quote_smart($value)
{
    // Stripslashes is gpc on
    if (get_magic_quotes_gpc())
    {
        $value = stripslashes($value);
    }
    // Quote if not a number or a numeric string
    if ( !is_numeric($value) )
    {
        $value = mysql_real_escape_string($value);
    }
    return $value;
}

?>

Ryaner
A: 

http://stackoverflow.com/editing-help

rt
A: 

I can think of a number of things that could cause this. But it depends how you are invoking SQL queries. If you moved to use parameterized queries like with PDO, then escaping is unnecessary which means the call to mysql_real_escape_string is adding the extra slashes.

If you are using mysql_query etc. then there must be some code somewhere like addslashes which is doing this. This could either be before the data is going into the database, or after.

Also you say you have disabled magic quotes... if you haven't already, just do a hard check in the code with something like this:

echo htmlentities($_GET['value']); // or $_POST, whichever is appropriate

Make sure there are no slashes in that value, then also check this:

echo "Magic quotes is " . (get_magic_quotes_gpc() ? "ON" : "OFF");

I know you've said multiple times it isn't magic quotes, but for us guys trying to help we need to be sure you have checked the actual PHP output rather than just changing the config (which might not have worked).

Mike Weller
A: 

Problem still exits

nreaction