views:

183

answers:

7

Hi

I made a simple news system with comments using PHP and MySQL, and it worked great on my local Apache server, both on my Fedora 10 machine, and my Windows 7 one. Now I have a problem though, I uploaded it to a web host and it keeps returning all the ' and " as \' and \".

I believe this is either the web host who by automatic adds them for security reasons or that the MySQL is the wrong collation, but I have not been able to resolve it yet, testing multiple MySQL collations.

Here is a example of a query:

mysql_query("INSERT INTO news (title, poster, text, time) VALUES ('$newstitle', '1', '$newstext', '$time')") or die(mysql_error());

$time is time(); $newstitle and $newstext are parsed from $_POST and both are ran through mysql_real_escape_string() before I run the query (I guess this might be the problem, but since it is a part of the security I just don't want to remove it, and since it did not cause problems on my local servers)

As a final note: On my local apache servers I had latin1_swedish_ci which did not work on the web hosts server.

EDIT:

They look like this in the database:

\'\'\'\"\"\"

While on my local ones they didn't have the extra backslash, so it must be the PHP adding it. Is there any way to solve this except adding a function that removes extra backslashes?

+4  A: 

Edit: As mentioned several times below, and in comments, the correct way to go about this is to disable magic quotes. Had I fully read and comprehended the question before replying, then that would probably have been better ;)


mysql_real_escape_string() will be doing what it says, and escaping the relevant characters within the string. Escaping quotes is done by prefixing them with the escape character (a back-slash in PHP, and many other languages). When you pull the data back from the database, do something like run stripslashes() (see the PHP doc) on the content, to remove the slashes from things like quotes.

Edit: As Gumbo mentioned, it could also be due to Magic Quotes being enabled on the server. You should disable these as per his answer.

James Burgess
Why didn't I have to do that with my local server? Just curious since it is the same code (except for mysql details)...
Your local server most likely has magic_quotes_gpc = off, while their server has magic_quotes_gpc = on.
R. Bemrose
Just edited the post, it could be that Magic Quotes were enabled (hat-tip to Gumbo for the reminder).
James Burgess
Speaking of Magic Quotes, I will be SO glad when this "feature" finally goes away in PHP6.
R. Bemrose
Couldn't agree more - one of the several improvements I'm looking forward to in PHP6!
James Burgess
Thanks for all the help, just one more thing, how do I disable magic quotes without proper access to the server? I only have FTP access to the upload directory. I read somewhere I can either do it by adding a php.ini file in each subdir, using .htaccess or do it in a the php files. Which would be the best way?
You can place the line "php_flag magic_quotes_gpc Off" in your .htaccess to disable it.
James Burgess
Thank you, will try!
Works perfectly now, thank you everyone who helped me!
It might be a better idea to select Gumbo's answer as the correct one, as he was the first to offer the correct solution.
James Burgess
Will do, I am new here so I really does not know how to do everything yet :P
+7  A: 

These additional backslashes are probably Magic Quotes. Try to disable them or remove them before processing the data.

Gumbo
This is actually the correct answer -- mysql_real_escape_string will escape the characters so that you can insert them, but those escapes won't be there when pulling data back from the db.
Rytmis
+2  A: 

Your host might have Magic Quotes enabled.

Kibbee
+2  A: 

Look at magic_quotes_gpc. You can turn this off via .htaccess and use mysql_real_escape_string().

Chris Bartow
+4  A: 

No, you don't want to run stripslashes(). Rather, set up your server properly.

First, turn off magic_quotes. This means that any data going into your script will have nothing escaped.

Then run mysql_real_escape_string() on the data, that'll add the slashes for the query only, and will store the data in the database without the slashes.

When you load the data again, you'll be good to go and there won't be any backslashes around the quotes.

leftnode
+1 the Right Way to fix this. :-)
Rytmis
+3  A: 

Take a look at your magic_quotes settings, and compare them on your servers. Your home servers probably have magic_quotes disabled (as they should be, since Magic Quotes is deprecated in current versions of PHP), and your web host likely has them enabled (which is sadly common). You can use:

<?php echo phpinfo(); ?>

for a quick comparison.

If magic_quotes is new to you, you may want to look into PHP security a bit more before you deploy anything you care about.

Questions containing "magic_quotes"

drfloob
A: 

The real solution is to use prepared statements in your insert queries.

Here's a site with an insert example right at the top.

Prepared Statements in PHP

The benefit is that it's a best practice, and the side-effect is that you no longer have to worry about cleansing the input against SQL injection attacks.

ShawnMilo
No. The problem were unwanted additional backslashes.
Gumbo
Yes, but this prevents them from ever getting in. Cleaning them up in the existing data is easy with a little code. If the inserts are being done properly, then the problem will not recur.
ShawnMilo
No. The additional backslashes were caused by Magic Quotes and not by mysql_real_escape_string. Thus prepared statements wouldn’t make a difference as the backslashes were already added by Magic Quotes.
Gumbo