PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Ocultando PHP> <Comillas Mágicas
Last updated: Fri, 22 Aug 2008

view this page in

Desactivación de Comillas Mágicas

Puede que la directiva magic_quotes_gpc solo pueda ser desactivada en el nivel de sistema, y no en tiempo de ejecución. En otras palabras, usar ini_set() no es posible.

Example #1 Desactivación de comillas mágicas del lado del servidor

Un ejemplo que define el valor de estas directivas a Off en php.ini. Para detalles adicionales, lea la sección del manual titulada Cómo modificar los parámetros de configuración.

; Magic Quotes
;

; Comillas mágicas para datos GET/POST/Cookie de entrada.
magic_quotes_gpc = Off

; Comillas mágicas para datos generados en tiempo de ejecución,
; p.ej. desde SQL, exec(), etc
magic_quotes_runtime = Off

; Usar comillas mágicas tipo Sybase (escapar ' con '' en lugar de \').
magic_quotes_sybase = Off

Si el acceso a la configuración del servidor no se encuentra disponible, el uso de .htaccess es también una opción. Por ejemplo:

php_flag magic_quotes_gpc Off

Con el propósito de escribir código portable (código que funciona en cualquier entorno), por ejemplo si la configuración en el nivel del servidor no es posible, he aquí un ejemplo de cómo deshabilitar magic_quotes_gpc en tiempo de ejecución. Este método es ineficiente así que es preferible definir las directivas apropiadas en algún otro lugar.

Example #2 Deshabilitar comillas mágicas en tiempo de ejecución

<?php
if (get_magic_quotes_gpc()) {
    function 
stripslashes_profundo($valor)
    {
        
$valor is_array($valor) ?
                    
array_map('stripslashes_profundo'$valor) :
                    
stripslashes($valor);

        return 
$valor;
    }

    
$_POST array_map('stripslashes_profundo'$_POST);
    
$_GET array_map('stripslashes_profundo'$_GET);
    
$_COOKIE array_map('stripslashes_profundo'$_COOKIE);
    
$_REQUEST array_map('stripslashes_profundo'$_REQUEST);
}
?>



Ocultando PHP> <Comillas Mágicas
Last updated: Fri, 22 Aug 2008
 
add a note add a note User Contributed Notes
Desactivación de Comillas Mágicas
stuart at horuskol dot co dot uk
25-Apr-2008 09:26
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.
17-Dec-2006 08:20
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);
}
sunrunner20
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
rdk
08-Sep-2006 07: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.
dedlfix
20-Aug-2006 01: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.

Ocultando PHP> <Comillas Mágicas
Last updated: Fri, 22 Aug 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites