PHP Velho Oeste 2024

Por que não usar Magic Quotes?

Aviso

Esta funcionalidade tornou-se OBSOLETA desde o PHP 5.3.0 e foi REMOVIDA desde o PHP 5.4.0.

  • Portabilidade Presumir que ela está ligada, ou desligada, afeta portabilitade. Use get_magic_quotes_gpc() para verificar isso e codifique de acordo com a situação.
  • Performance Como nem todos os dados escapados são inseridos em um banco de dados, existe uma perda de performance por escapar todos os dados. Chamar funções de escape (como addslashes()) em tempo de execução é mais eficiente. Embora o arquivo php.ini-dist habilita essas diretivas por padrão, php.ini-recommended desabilita ela. Essa recomendação é principalmente por razões de performance.
  • Inconviniência Porque nem todos os dados precisam ter caracteres de escape inseridos, é irritante ver caracteres de escape onde não deviam. Por exemplo, mandar um e-mail por um formulário, e ver um monte de \' na mensagem. Para consertar isso, pode ser necessário o uso excessivo da função stripslashes().
add a note add a note

User Contributed Notes 5 notes

up
11
rjh at netcraft dot com
16 years ago
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().
up
11
anze
15 years ago
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.
up
5
Albin Mrtensson
13 years ago
This is what I use to handle magic quotes

<?php

if (get_magic_quotes_gpc()) {
    function
strip_array($var) {
        return
is_array($var)? array_map("strip_array", $var):stripslashes($var);
    }

   
$_POST = strip_array($_POST);
   
$_SESSION = strip_array($_SESSION);
   
$_GET = strip_array($_GET);
}

?>
up
1
Anonymous
18 years ago
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.
up
-6
sir dot steve dot h+php at gmail dot com
16 years ago
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.
To Top