I want to turn off PHP's magic quotes. I don't have access to php.ini.
When I tried to add php_flag magic_quotes_gpc off
to my .htaccess file, I get a 500 internal server error. This is what my .htaccess file looks like:
AddType x-mapp-php5 .php
php_flag magic_quotes_gpc off
Then I tried to use ini_set('magic_quotes_gpc', 'O')
, but that had no effect.
How do I turn magic quotes off?
BaileyP's answer is already pretty good, but I would use this condition instead:
It is more defensive.
As per the manual you can often install a custom php.ini on shared hosting, where mod_php isn't used and the
php_value
directive thus leads to an error. For suexec/FastCGI setups it is quite common to have a per-webspacephp.ini
in any case.--
I don't think O (uppercase letter o) is a valid value to set an ini flag. You need to use a true/false, 1/0, or "on"/"off" value.
EDIT
After checking the list of ini settings, I see that magic_quotes_gpc is a
PHP_INI_PERDIR
setting (after 4.2.3), which means you can't change it withini_set()
(onlyPHP_INI_ALL
settings can be changed withini_set()
)What this means is you have to use an .htaccess file to do this - OR - implement a script to reverse the effects of magic quotes. Something like this
I know I'm late to answer this, but I read most of the answers and while many were great, only djn actually explained why you were getting this
500 Internal Server Error
.While his explanation was 100% correct, this is a perfect example of why you should always wrap those in an
<IfModule>
. While this won't fix the actual problem of not being able to set those flags in your.htaccess
, it will at least prevent the500
error.Or for older versions it would be
<IfModule mod_php.c>
etc.I try to make a habit out of always doing this so as to avoid any such 500 errors. After that, just apply what Peter Bailey said.
While I can't say why php_flag is giving you
500 Internal Server Error
s, I will point out that the PHP manual has an example of detecting if magic quotes is on and stripping it from the superglobals at runtime. Unlike the others posted, this one is recursive and will correctly strip quotes from arrays:Update: I noticed today that there's a new version of the following code on the PHP manual that uses references to the super-globals instead.
Old version:
New version:
Does it work if you remove the AddType line? I'm not quite sure why that's relevant to turning magic quotes off.
If PHP isn't running under mod_php, htaccess won't work. Does it work as a CGI?
This is one for your hosting company really.
If you're running PHP 5.3+ this will do the trick, place it at the topmost of your page:
Handles keys, values and multi-dimensional arrays.