views:

26

answers:

2

I'm using a web form to store user input into a MySQL DB, using $_POST.

I have noticed that once textarea fields area read, slashes are inserted automatically to escape some characters.

As I need to manipulate text before storing, I thought about using stripslashes, however I have discovered that it may garbage text, if Japanese or other asiatic character sets are used.

Is there a proper way to do the job (remove slashes) safely?

+2  A: 

Turn off magic quotes in your PHP settings. The feature is deprecated anyway.

David Dorward
I have checked out php.ini, it looks like magic quotes is disabled:<code>; Magic quotes;; Magic quotes for incoming GET/POST/Cookie data.magic_quotes_gpc = Off; Magic quotes for runtime-generated data, e.g. data from SQL, from exec(), etc.magic_quotes_runtime = Off; Use Sybase-style magic quotes (escape ' with '' instead of \').magic_quotes_sybase = Off</code>
Riccardo
Yea, that's the default settings in PHP5.3, because as said magic quotes are deprecated. In this case you SHOULDN'T use stripslashes on the string! stipslashes is only necessary if magic quotes are enabled and that's actually what I've posted in my anwser. If magic quotes are on, stripslashes will be used, if not they won't. If for any reason you have to port your code to a server where magic quotes are enabled and you can't or aren't allowed to disable them (i.e. cause they disturb other scripts) than my solution will still work, while the above one won't.
Tseng
+1  A: 

If you don't want to disable magic quotes (even though they won't be working anymore in PHP 6.0 as they were deprecated in 5.3), you can use this code

$txt = $_POST['txt'];
if(get_magic_quotes_gpc())
    $txt = stripslashes($txt);

this way stripslashes would only be enabled when your PHP module has magic quotes enabled or ignore it otherwise

Tseng
As I wrote, stripslashes seems dangerous for some asian-encoded languages, like Japanese....
Riccardo