Why are $_POST variables getting escaped in PHP?

You probably have magic quotes enabled on the Linux server: magic_quotes

When magic_quotes are on, all ‘ (single-quote), ” (double quote), \ (backslash) and NUL’s are escaped with a backslash automatically.

They’re a good thing to disable, as they are going to be removed from PHP 6 onwards anyway. You should also be able to disable them inside your script: set-magic-quotes-runtime You can’t deactivate the part of magic_quotes responsible for escaping POST data during runtime. If you can, disable it in php.ini. If you can’t do that, do a check whether the magic_quotes are enabled, and do a stripslashes() on any content you fetch from POST:

if (get_magic_quotes_gpc())  
 $my_post_var = stripslashes($_POST["my_post_var"]);

Leave a Comment