User Tools

Site Tools


killmagicquotes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
killmagicquotes [2006/11/13 14:43]
206.161.192.10
killmagicquotes [2006/11/13 14:48] (current)
andi old revision restored
Line 1: Line 1:
 +====== Remove magic quotes ======
  
 +**Where do I insert this code? **\\
 +Before any code that reads from any of those contexts being cleaned, IOW, in the begining of your PHP code.
 +----
 +
 +<code php>
 +if (get_magic_quotes_gpc()) {
 +  if (!empty($_GET)) ​   remove_magic_quotes($_GET);​
 +  if (!empty($_POST)) ​  ​remove_magic_quotes($_POST);​
 +  if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);​
 +  if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);​
 +  if (!empty($_SESSION)) remove_magic_quotes($_SESSION);​
 +  ini_set('​magic_quotes_gpc',​ 0);
 +}
 +set_magic_quotes_runtime(0);​
 +
 +function remove_magic_quotes(&​$array) {
 +  foreach (array_keys($array) as $key) {
 +    if (is_array($array[$key])) {
 +      remove_magic_quotes($array[$key]);​
 +    }else {
 +      $array[$key] = stripslashes($array[$key]);​
 +    }
 +  } 
 +}
 +</​code>​
 +
 +Or you could do it like this:
 +<code php>
 +if (get_magic_quotes_gpc()) ​
 +{
 +    foreach (array('​GET',​ '​POST',​ '​COOKIE',​ '​REQUEST',​ '​SESSION'​) as $global)
 +    {
 +        $global = "​_$global";​
 +        empty($$global) || $$global = array_map('​safe_stripslashes',​ $$global)
 +    }
 +    ini_set('​magic_quotes_gpc',​ 0);
 +}
 +
 +set_magic_quotes_runtime(0);​
 +
 +function safe_stripslashes($element)
 +{
 +    if (is_array($element))
 +    {
 +        return array_map('​safe_stripslashes',​ $element);
 +    }
 +    // else
 +
 +    return stripslashes($element);​
 +}
 +</​code>​
killmagicquotes.txt ยท Last modified: 2006/11/13 14:48 by andi