get_magic_quotes_gpc
取得 PHP 环境变数 magic_quotes_gpc 的值,属于 PHP 系统功能。
语法: long get_magic_quotes_gpc(void);
返回值: 长整数
这个函数做什么的?
本函数取得 PHP 环境配置的变量 magic_quotes_gpc (GPC, Get/Post/Cookie) 值。返回 0 表示关闭本功能;返回 1 表示本功能打开。当 magic_quotes_gpc 打开时,所有的 ' (单引号), " (双引号), (反斜线) and 空字符会自动转为含有反斜线的溢出字符。
在php的配置文件中,有个布尔值的设置,就是magic_quotes_runtime。当它打开时,php的大部分函数自动的给从外部引入的(包括数据库或者文件)数据中的溢出字符加上反斜线。 当然如果重复给溢出字符加反斜线,那么字符串中就会有多个反斜线,所以这时就要用set_magic_quotes_runtime()与get_magic_quotes_runtime()设置和检测php.ini文件中magic_quotes_runtime状态。
为了使自己的程序不管服务器是什么设置都能正常执行。可以在程序开始用get_magic_quotes_runtime检测该设置的状态决定是否要手工处理,或者在开始(或不需要自动转义的时候)用set_magic_quotes_runtime(0)关掉该设置。
magic_quotes_gpc设置是否自动为GPC(get,post,cookie)传来的数据中的'"\加上反斜线。可以用get_magic_quotes_gpc()检测系统设置。如果没有打开这项设置,可以使用addslashes()函数添加,它的功能就是给数据库查询语句等的需要在某些字符前加上了反斜线。这些字符是单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符)。
一般用法如下:
1 | if (!get_magic_quotes_gpc()) |
在手册中string addslashes ( string str )介绍的时候有这样一句话说明了get_magic_quotes_gpc的用法以及作用。默认情况下,PHP 指令 magic_quotes_gpc 为 on,它主要是对所有的 GET、POST 和 COOKIE 数据自动运行 addslashes()。不要对已经被 magic_quotes_gpc 转义过的字符串使用 addslashes(),因为这样会导致双层转义。遇到这种情况时可以使用函数 get_magic_quotes_gpc() 进行检测。
其实这个函数就是判断PHP有没有自动调用addslashes 这个函数:
最土团购系统里的magic_gpc
02 | define( 'SYS_MAGICGPC' , get_magic_quotes_gpc()); |
04 | $_POST = magic_gpc( $_POST ); |
06 | function magic_gpc( $string ) { |
08 | if ( is_array ( $string )) { |
09 | foreach ( $string as $key => $val ) { |
10 | $string [ $key ] = magic_gpc( $val ); |
13 | $string = stripslashes ( $string ); |
19 | echo 'get_magic_quotes_gpc的值: ' .get_magic_quotes_gpc(); |
21 | echo '直接输出POST变量: ' . $_POST [ 'nowamagic' ]; |
23 | echo '经过magic_gpc处理: ' .magic_gpc( $_POST [ 'nowamagic' ]); |
28 | <form action= "<?=$_SERVER['PHP_SELF']?>" method= "post" > |
29 | <input type= "text" name= "nowamagic" value= "no'wamagic.net" > |
30 | <input type= "submit" value= "提交" > |
程序输出:
1 | get_magic_quotes_gpc的值: 1 |
2 | 直接输出POST变量: no'wamagic.net |
3 | 经过magic_gpc处理: no'wamagic.net |
再来个例子:
03 | echo 'get_magic_quotes_gpc: ' .get_magic_quotes_gpc(); |
05 | echo '直接输出POST变量: ' . $_POST [ 'nowamagic' ]; |
07 | echo 'addslashes: ' . addslashes ( $_POST [ 'nowamagic' ]); |
09 | if (!get_magic_quotes_gpc()) { |
10 | $nowamagic = addslashes ( $_POST [ 'nowamagic' ]); |
13 | $nowamagic = $_POST [ 'nowamagic' ]; |
17 | echo '处理后输出: ' . $nowamagic ; |
22 | <form action= "<?=$_SERVER['PHP_SELF']?>" method= "post" > |
23 | <input type= "text" name= "nowamagic" value= "no'wamagic.net" > |
24 | <input type= "submit" value= "提交" > |
程序输出:
2 | 直接输出POST变量: no'wa\magic.net |
3 | addslashes : no\'wa\\magic.net |
没有帐号? 现在注册.