关闭PHP魔术引号(Magic Quote)

PHP的魔术引号给程序编写带来了一丁点方便,但却严重影响的程序的可移植性,在开启magic_quotes_gpc的环境下写的程序到没开启的环境下就有可能出现错误。所以通常情况下,我们会关闭magic_quotes_gpc以避免它带来的问题。

手册里介绍了三种方法,在些整理一下:

1 修改PHP配置文件php.ini

这种方法只适于自己有权管理服务器的情况下,如果用的虚拟空间,那就只能采用后两条方法。

在PHP配置文件php.ini中将magic_quotes_gpc、magic_quotes_runtime、magic_quotes_sybase全部设为off。如下所示:

; Magic quotes

;

; Magic quotes for incoming GET/POST/Cookie data.

magic_quotes_gpc = Off

; Magic quotes for runtime-generated data, e.g. data from SQL, from exec(), etc.

magic_quotes_runtime = Off

; Use Sybase-style magic quotes (escape ‘ with ” instead of ‘).

magic_quotes_sybase = Off

2 利用.htaccess文件

该方法只在服务器支持htaccess的情况下,现在的服务器一般都会支持的

在程序目录下.htaccess文件中增加下面一句:

php_flag magic_quotes_gpc Off

 

3 在代码中屏蔽

该方法是移植性最强的,不用考虑服务器的配置,只要支持PHP就可以使用。

在所有PHP文件开始处增加下面代码


<?php
if (get_magic_quotes_gpc()) {
    function stripslashes_deep($value)
    {
        $value = is_array($value) ?
                    array_map('stripslashes_deep', $value) :
                    stripslashes($value);

        return $value;
    }

    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
    $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}
?> 


( ! ) Warning: Missing argument 1 for cwppos_show_review(), called in /data/www/enjoyphp/wp-content/themes/flat/content-single.php on line 29 and defined in /data/www/enjoyphp/wp-content/plugins/wp-product-review/includes/legacy.php on line 18
Call Stack
#TimeMemoryFunctionLocation
10.0000354984{main}( ).../index.php:0
20.0001355272require( '/data/www/enjoyphp/wp-blog-header.php' ).../index.php:17
30.177610160368require_once( '/data/www/enjoyphp/wp-includes/template-loader.php' ).../wp-blog-header.php:19
40.178910231512include( '/data/www/enjoyphp/wp-content/themes/flat/single.php' ).../template-loader.php:74
50.274610802896get_template_part( ).../single.php:5
60.274610803416locate_template( ).../general-template.php:167
70.274610803512load_template( ).../template.php:643
80.274610803896require( '/data/www/enjoyphp/wp-content/themes/flat/content-single.php' ).../template.php:686
90.280310789008cwppos_show_review( ).../content-single.php:29

1 评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注