title: Best method of disabling php magic quotes without php.ini or .htaccess
body: <p>I am needing to write portable code that will run on a shared server with <code>magic_qoutes_gpc</code> enabled and I am unable to change that in php.ini or .htaccess. (the server is running php 5.2)</p>
  
  <p>It seems there are numerous functions to stripslaches from all of the <code>$_GET</code>, <code>$_POST</code> etc superglobals but I'm not sure which is the best. Also some comments <a href="http://php.net/manual/en/security.magicquotes.disabling.php" rel="nofollow noreferrer">here</a> seem to say that the keys also have slashes added which need to be stripped as well. So should I use the one on the PHP website:</p>
  
  <pre><code>if (get_magic_quotes_gpc()) {
      $process = array(&amp;$_GET, &amp;$_POST, &amp;$_COOKIE, &amp;$_REQUEST);
      while (list($key, $val) = each($process)) {
          foreach ($val as $k =&gt; $v) {
               unset($process[$key][$k]);
              if (is_array($v)) {
                  $process[$key][stripslashes($k)] = $v;
                  $process[] = &amp;$process[$key][stripslashes($k)];
              } else {
                  $process[$key][stripslashes($k)] = stripslashes($v);
              }
          }
      }
      unset($process);
  }
  </code></pre>
  
  <p>or something like this: (from this answer: <a href="https://stackoverflow.com/questions/2077711/php-shorter-magic-quotes-solution">PHP - Shorter Magic Quotes Solution</a>)</p>
  
  <pre><code>function strip_slashes_recursive(&amp;$value) {
      if (!is_array($value)) {
          $value = strip_slashes($value);
      } else {
          foreach (array_keys($value) as $key) {
              $arrayValue = strip_slashes_recursive($value[$key]);
              unset($value[$key]);
              $value[strip_slashes($key)] = $arrayValue;
          }
      }
  }
  
  foreach (array(&amp;$_GET, &amp;$_POST, &amp;$_COOKIE, &amp;$_REQUEST) as &amp;$array) {
      strip_slashes_recursive($array);
  }
  // don't forget to unset references or it can lead to very nasty bugs
  unset($array);
  </code></pre>
  
  <p>or even something like this:</p>
  
  <pre><code>if (get_magic_quotes_gpc()) {
      function undoMagicQuotes($array, $topLevel=true) {
          $newArray = array();
          foreach($array as $key =&gt; $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);
  }
  </code></pre>
  
  <p>Can someone explain the pros/cons of each approach and/or a totally different approach and how thorough they are and if they strip slashes from the key as well as the value.</p>
  
  <p>(also is this method any good: <a href="https://stackoverflow.com/questions/2133026/php-how-to-correctly-remove-escaped-quotes-in-arrays-when-magic-quotes-are-on">PHP: how to (correctly) remove escaped quotes in arrays when Magic Quotes are ON</a>)<br>
  (and also it seems like all of these methods are incomplete as they don't strip slashes from all the affected superglobals <a href="https://stackoverflow.com/questions/2024150/which-superglobals-are-affected-by-magic-quotes-gpc-1">Which superglobals are affected by magic_quotes_gpc = 1?</a>)</p>