关于php中的magic_quotes_gpc和safe_mode

今天重新配置了下php ,查看apache log却发现如下警告信息:

PHP Warning: Directive ‘safe_mode’ is deprecated in PHP 5.3 and greater in Unknown on line 0
PHP Warning: Directive ‘magic_quotes_gpc’ is deprecated in PHP 5.3 and greater in Unknown on line 0

官方看了下,用红色的block特别注明了,php5.3已经不推荐使用这个东东了,在 PHP6 中已经将其废弃:

magic_quotes_gpc boolean
Warning
This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.

magic_quotes_gpc=on 的配置下,插入数据时,Magic quotes 会自动将数据转义。 可以从一定程度上,让初学者带离脚本的安全风险。例如在没有任何保护措施的代码下,开启了 Magic quotes 后会少很多的风险,例如注入问题。以前看一些安全方面的文章,入侵者总是喜欢选择magic_quotes_gpc=off的站下手,如果看到其magic_quotes_gpc=on ,估计就不会弄这个站了。
那么官方为什么要废除这个东东呢?搜索了一下,看到一个写得相当详细和全面的帖子,下面将其中提到的几个原因帖出:
为什么不使用 Magic quotes

可移植性
无论此功能是否开启,它都会影响脚本的可移植性,因为它影响我们后续过滤数据的操作。

性能问题
在获取所有的外部数据之前都会被转义,这无疑会增加运行时的花销(而且并不是所有的数据都需要转义)。

造成困惑
正如上述所言,并非所有的数据都需要被转义。有可能出现的一种情况,就是当你为了获取未被转义的数据,而“疯狂的”使用 stripslashes 函数。

如何禁用 magic_quotes_gpc 和safe_mode
1,用 php.ini 配置文件全局禁用

1
2
magic_quotes_gpc = Off
safe_mode = Off

2,使用 .htaccess 文件禁用(对于虚拟主机)

1
2
php_flag magic_quotes_gpc Off
php_flag safe_mode Off

禁用了这两个东东后,安全问题就更加不能忽视了。在数据入库前一定要addslashes ,出库后要记得stripslashes 。

看了下phpwind和 discuz的源码 ,发现phpwind就是直接用的addslashes和stripslashes 。而dz则是自己定义了两个函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    function daddslashes($string, $force = 0)
    {
    !defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
    if(!MAGIC_QUOTES_GPC || $force)
    {
        if(is_array($string))
        {
            foreach($string as $key => $val)
            {
                $string[$key] = daddslashes($val, $force);
            }
        } else
        {
            $string = addslashes($string);
        }
    }
    return $string;
    }
   
        function dstripslashes($string)
        {
        if(is_array($string))
        {
            foreach($string as $key => $val)
            {
                $string[$key] = dstripslashes($val);
            }
        }
        else
        {
            $string = stripslashes($string);
        }
        return $string;
        }

dz的这两个函数的在原函数的基础上扩充了对数组数据的支持,用起来更方便。不过dz的这两个函数不够简洁,这里我给出两个简洁点的:

1
2
3
4
5
6
7
8
9
10
11
12
function addslashes_deep($string)
  {
    $string = is_array($string)?array_map('addslashes_deep', $string):addslashes($string);
    return $string;  
  }
 
 
    function stripslashes_deep($string)
  {
    $string = is_array($string)?array_map('stripslashes_deep', $string):stripslashes($string);
    return $string;  
  }

在数据入库前和出库后都要记得:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//入库前
if(!get_magic_quotes_gpc())
{
$_GET=addslashes_deep($_GET);
$_POST=addslashes_deep($_POST);
$_REQUEST=addslashes_deep($_REQUEST);
//其它要处理的变量.......
}

//出库后
if(get_magic_quotes_gpc())
{
$_GET=stripslashes_deep($_GET);
$_POST=stripslashes_deep($_POST);
$_REQUEST=stripslashes_deep($_REQUEST);
//其它要处理的变量.......
}

喜欢这篇文章吗?

请订阅本站 RSS feed填写您的邮件地址,订阅我们的精彩内容:

随机日志

回复 (1)

  1. PHPer  / 回复

    好文, 正需要呢, 谢谢!

发表评论 修改评论取消编辑

允许使用的标签 - 您可以在评论中使用如下的 HTML 标签以及属性。

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 :wink:  :-|  :-x  :twisted:  :)  8-O  :(  :roll:  :-P  :oops:  :-o  :mrgreen:  :lol:  :idea:  :-D  :evil:  :cry:  8)  :arrow:  :-?  :?:  :!:

引用通告 (0)

› 尚无引用通告。

开灯