I have discovered that my host doesn't like either of the following directives in the .htaccess file:
php_flag magic_quotes_gpc Off
php_value magic_quotes_gpc Off
However, there is another way to disable this setting even if you don't have access to the server configuration - you can put a php.ini file in the directory where your scripts are with the directive:
magic_quotes_gpc = Off
However, these does not propogate unlike .htaccess rules, so if you launch from a sub-directory, you need the php.ini file in each directory you have as script entry points.
Desabilitando Magic Quotes
A diretiva magic_quotes_gpc só pode ser desabilita em nível de sistema, e não em tempo de execução. Em outras palavras, uso da função ini_set() não é uma opção.
Exemplo #1 Desabilitando magic quotes no lado do servidor
Um exemplo que configuração dessa diretiva para Off (Desligada) no arquivo php.ini. Para detalhes adicionais, leia a seção do manual entitulada Como mudar os valores das configurações.
; Magic quotes ; ; Magic quotes para dados vindos via GET/POST/Cookie. magic_quotes_gpc = Off ; Magic quotes para dados gerados em tempo de execução,ex.: dados vindo de SQL, de chamadas à exec(), etc. magic_quotes_runtime = Off ; Usar magic quotes no estilo Sybase (escapar ' com '' ao invés de \'). magic_quotes_sybase = Off
Se acesso à configuração do servidor não estiver disponível, uso do arquivo .htaccess também é uma opção. Por exemplo:
php_flag magic_quotes_gpc Off
No interesse de escrever código portável (código que funciona em qualquer ambiente), como onde configurar a opção em nível de servidor não for possível, aqui está um exemplo de como desabilitar magic_quotes_gpc em tempo de execução. Esse método é ineficiente então é preferível configurar as diretivas apropriadas em outros lugares.
Exemplo #2 Disabilitando magic quotes em tempo de execução
<?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);
}
?>
Desabilitando Magic Quotes
25-Apr-2008 08:26
PHP's magic quotes function has the strange behavior of not adding slashes to top level keys in GPC key/value pairs but adding the slashes in deeper level keys. To demonstrate, a URI of:
example.php?a'b[c'd]=e'f
produces:
array("a'b" => array("c\'d" => "e\'f"))
The current example for removing magic quotes does not do anything to keys, so after running stripslashes_deep, you would end up with:
array("a'b" => array("c\'d" => "e'f"))
Which, needless to say, is wrong. As if you had magic quotes off, it would have been:
array("a'b" => array("c'd" => "e'f"))
I have written a snippet of code compatible with PHP 4.0.0 and above that handles this correctly:
if (get_magic_quotes_gpc()) {
function undoMagicQuotes($array, $topLevel=true) {
$newArray = array();
foreach($array as $key => $value) {
if (!$topLevel) {
$key = stripslashes($key);
}
if (is_array($value)) {
$newArray[$key] = undoMagicQuotes($value, false);
}
else {
$newArray[$key] = stripslashes($value);
}
}
return $newArray;
}
$_GET = undoMagicQuotes($_GET);
$_POST = undoMagicQuotes($_POST);
$_COOKIE = undoMagicQuotes($_COOKIE);
$_REQUEST = undoMagicQuotes($_REQUEST);
}
25-Nov-2006 03:10
If php_flag magic_quotes_gpc off does not work
Use php_value magic_quotes_gpc off
insteadin your .htaccess file
08-Sep-2006 06:44
The function parse_str() (http://us3.php.net/manual/en/function.parse-str.php) is also affected by magic_quotes_gpc, so if that function is called anywhere, stripslashes_deep won't be sufficient by itself.
20-Aug-2006 12:18
The function stripslashes_deep() ignores slashes in the keys
For example a query string like this: ?foo'bar=baz'bal
Output of var_dump($_GET) is:
array(1) {
["foo\'bar"]=>
string(8) "baz\'bal"
}
after stripslashes_deep():
array(1) {
["foo\'bar"]=>
string(7) "baz'bal"
}
If you want the keys to be stripslashed too, you have to unset() the addslahed key and to add a stripslashed version. But keep in mind that this will change the order of the array.
