How can I disable PHP magic quotes at runtime? How can I disable PHP magic quotes at runtime? php php

How can I disable PHP magic quotes at runtime?


Only magic_quoted_runtime can be disabled at runtime. But magic_quotes_gpc can’t be disabled at runtime (PHP_INI_ALL changable until PHP 4.2.3, since then PHP_INI_PERDIR); you can only remove them:

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);}

For further information see Disabling Magic Quotes.


Magic quotes cannot be disabled at runtime, but you can use a .htaccess file in the directory to disable it.

php_flag magic_quotes_gpc off

The only real advantage this has is you can put it once in a directory and it works for the whole directory and subdirectories. Really nice if you need this for an application you didn't write and need to get it to work without magic quotes.


I have a little script for this similar to Gumbo's (but of course I like mine better :):

if(function_exists('get_magic_quotes_runtime') && get_magic_quotes_runtime())    set_magic_quotes_runtime(false);if(get_magic_quotes_gpc()) {    array_stripslashes($_POST);    array_stripslashes($_GET);    array_stripslashes($_COOKIES);}function array_stripslashes(&$array) {    if(is_array($array))        while(list($key) = each($array))            if(is_array($array[$key]))                array_stripslashes($array[$key]);            else                $array[$key] = stripslashes($array[$key]);}