How to turn off magic quotes on shared hosting?
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?
Solution 1:
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-webspace php.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.
ini_set( 'magic_quotes_gpc', 0 ); // doesn't work
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 with ini_set()
(only PHP_INI_ALL
settings can be changed with ini_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
if ( in_array( strtolower( ini_get( 'magic_quotes_gpc' ) ), array( '1', 'on' ) ) )
{
$_POST = array_map( 'stripslashes', $_POST );
$_GET = array_map( 'stripslashes', $_GET );
$_COOKIE = array_map( 'stripslashes', $_COOKIE );
}
Solution 2:
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:
<?php
if (get_magic_quotes_gpc()) {
function stripslashes_deep($value)
{
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
$_POST = array_map('stripslashes_deep', $_POST);
$_GET = array_map('stripslashes_deep', $_GET);
$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
$_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}
?>
New version:
<?php
if (get_magic_quotes_gpc()) {
$process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
while (list($key, $val) = each($process)) {
foreach ($val as $k => $v) {
unset($process[$key][$k]);
if (is_array($v)) {
$process[$key][stripslashes($k)] = $v;
$process[] = &$process[$key][stripslashes($k)];
} else {
$process[$key][stripslashes($k)] = stripslashes($v);
}
}
}
unset($process);
}
?>
Solution 3:
This will solve the problem of getting "Class 'PDO' not found" when you create a local php.ini file.
If you can't turn off magic quotes using the htaccess file (for reasons already given by Pete Bailey) just:
- Create a text file
- Rename it to 'php.ini'
-
Add the lines
magic_quotes_gpc = Off
magic_quotes_runtime = Off
magic_quotes_sybase = Off
extension=pdo.so
extension=pdo_mysql.so Save it to the directory/ies in which your scripts are executing.
Update: if you want to have just one copy of the new php.ini file then add this line to your root .htaccess file:
SetEnv PHPRC /path/to/site/root/public_html/php.ini
Obviously you need to move the ini file to this location of it's not there already.
Hope that saves someone the 2 hours it's just taken me!
Solution 4:
The php_flag and php_value inside a .htaccess file are technically correct - but for PHP installed as an Apache module only. On a shared host you'll almost never find such a setup; PHP is run as a CGI instead, for reasons related to security (keeping your server neighbours out of your files) and the way phpsuexec runs scripts as 'you' instead of the apache user.
Apache is thus correct giving you a server error: it doesn't know about the meaning of php_flag unless the PHP module is loaded. A CGI binary is to Apache an external program instead, and you can't configure it from within Apache.
Now for the good news: you can set up per-directory configuration putting there a file named 'php.ini' and setting there your instructions using the same syntax as in the system's main php.ini. The PHP manual lists all settable directives: you can set those marked with PHP_INI_PERDIR or PHP_INI_ALL, while only the system administrator can set those marked PHP_INI_SYSTEM in the server-wide php.ini.
Note that such php.ini directives are not inherited by subdirectories, you'll have to give them their own php.ini.
Solution 5:
======================== =============== MY SOLUTION ============================ (rename your php.ini to php5.ini)
and in the top (!), add these:
magic_quotes_gpc = Off
magic_quotes_runtime = Off
magic_quotes_sybase = Off
extension=pdo.so
extension=pdo_mysql.so
then in .htaccess, add this (in the top):
SetEnv PHPRC /home/your_path/to/public_html/php5.ini
p.s. change /home/your_path/to/
correctly (you can see that path by executing the <?php phpinfo(); ?>
command from a typical .php file.)