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

search for in the

Désactiver les guillemets magiques> <Pourquoi utiliser les guillemets magiques?
Last updated: Fri, 14 Nov 2008

view this page in

Pourquoi ne pas utiliser les guillemets magiques?

  • Portabilité Cette directive peut être activée ou désactivée suivant les serveurs et cela affecte grandement la portabilité. Utilisez get_magic_quotes_gpc() pour vérifier s'ils sont actifs ou pas, et adaptez votre application.
  • Performances Comme ce n'est pas toutes les données qui sont finalement placées dans une base, il y a un coût en vitesse pour protéger toutes ces données. Le simple appel des fonctions de protections en fonction des besoins est plus efficace (addslashes()). Même si php.ini-dist active ces options par défaut, php.ini-recommended les désactive. Cette recommendation est surtout faite pour des raisons de vitesse.
  • Peu pratique Comme toutes les données n'ont pas forcément besoin de protection, il est souvent désagréable de voir des données protégées là où ça ne sert à rien. Par exemple, lorsque vous envoyez par mail un formulaire, et que vous voyez des antislashs parsemer le message. Pour corriger cela, il faut faire un usage fréquent de stripslashes().


add a note add a note User Contributed Notes
Pourquoi ne pas utiliser les guillemets magiques?
anze
13-Oct-2008 10:23
Another reason against it: security. You could be lulled in a feeling of false security if you have magic_quotes=On on a test server and Off on production server.

And another: readability of the code. If you want to be portable you need to resort to some weird solution, outlines on these pages (if (get_magic_quotes())...).

Let's hope magic_quotes soon goes to history together with safe_mode and similar "kind-of-security" (but in reality just a nuisance) inventions.
estoesunapija at hotmail dot com
15-Sep-2008 05:26
<?php

//One could use array_walk, altough i think it was fun
//and simple doing it this way.

 
class oop {

     
//toObject        : Transforms an array into an object filtering it
      //$source         : Array to transform
      //$currentLevel : See $maxLevels
      //$maxLevels   : Protect the system in case of lots of recursion
      //                     i.e. <input type="text" name="test[][]....[N]"

     
public static function toObject($source=array(),$array=array(),$maxLevels=3,$currentLevel=0) {

         if ( !
sizeof($source) || ($currentLevel > $maxLevels) ) return FALSE;

        
$array   =  (sizeof($array)) ? $array : $source;
        
$obj     =  new stdClass();

         foreach (
$array as $k => $v){

            if (
is_array($v)) {

              
$obj->$k self::toObject($source,$v,$maxLevels,++$currentLevel);

               continue;

            }

           
//Assign to the object $obj, the key and the value of the actual value of $source

           
$obj->$k=$v;

         }

         return
$obj;

      }

   }

  
/* Eexamples

      $post    =  oop::toObject($_POST)   ;
      $get     =  oop::toObject($_GET)    ;
      $session =  oop::toObject($_SESSION);

      var_dump ($post)   ;
      var_dump ($get)    ;
      var_dump ($session);

   */

?>
sir dot steve dot h+php at gmail dot com
07-Dec-2007 04:45
I find it useful to define a simple utility function for magic quotes so the application functions as expected regardless of whether magic_quotes_gpc is on:

function strip_magic_slashes($str)
{
    return get_magic_quotes_gpc() ? stripslashes($str) : $str;
}

Which can be annoying to add the first time you reference every $_GET /$_POST/$_COOKIE variable, but it prevents you from demanding your users to change their configurations.
rjh at netcraft dot com
13-Jun-2007 09:50
Additionally, addslashes() is not a cure-all against SQL injection attacks. You should use your database's dedicated escape function (such as mysql_escape_string) or better yet, use parameterised queries through mysqli->prepare().
gerard at modusoperandi dot com dot au
14-May-2007 04:53
Apparently it will be removed in PHP 6:

http://www.php.net/~derick/meeting-notes.html#magic-quotes
11-Feb-2006 09:47
It is also important to disable Magic Quotes while in development enivronment. For the reasons mentioned above, not everybody is using Magic Quotes.

An application that works fine with Magic Quotes enabled may have security problems (ie can be subject to SQL attacks) when distributed.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites