magic_quotes_runtime 和 magic_quotes_gpc 的使用
来源:互联网 发布:淘宝客店铺软件哪个好 编辑:程序博客网 时间:2024/05/16 10:32
magic_quotes_runtime :PHP 配置参数, 默认为0,在PHP 5.3.0 以前不赞成使用,从PHP 5.4.0开始被废弃,get_magic_quotes_runtime() 总是返回 false 。
magic_quotes_runtime 开启与关闭的影响,举例:
新建文件:content.txt ,内容是:00'00"00\00+D:\PHP\TP\Library\Think
新建 test.php 文件,执行以下代码对文件内容进行读取:
$file_path = "./content.txt";$con = file_get_contents($file_path);echo $con;
当 magic_quotes_runtime 为 Off 时,输出:00'00"00\00+D:\PHP\TP\Library\Think
当 magic_quotes_runtime 为 On 时,输出:00\'00\"00\\00+D:\\PHP\\TP\\Library\\Think
可以看出,对于从文件获取的数据信息里获得的单引、双引、反斜线的处理是不同的。
如果magic_quotes_runtime启用,大多数函数从任何外部来源(包括数据库和文本文件)所返回的数据里的单引、双引、反斜线将不会被过滤掉,PHP会自动给它们加上反斜线进行转义。如果关闭,则不会加上。这样的符号在向数据库和文件进行写入、读取 时可能会出现两次转义及其他问题。在项目中希望手动处理所有的这些符号时,需要关闭magic_quotes_runtime。受到影响的函数主要是文件读取类的和数据库查询类的函数,即该配置作用范围:读取文件、读取数据。
set_magic_quotes_runtime(0);//关闭
set_magic_quotes_runtime(1);//开启
get_magic_quotes_runtime(), 返回 0 表示关闭本功能;返回 1 表示本功能打开。
如果对PHP版本升级,则项目可能会出现:Deprecated: Function set_magic_quotes_runtime() is deprecated 错误。解决方法:
@set_magic_quotes_runtime(0); 或 ini_set("magic_quotes_runtime", 0); 或 if (phpversion() < '5.3.0') set_magic_quotes_runtime(0);
===========================================================================================
magic_quotes_gpc
作用范围:当开启时,对所有的 GET、POST 和 COOKIE 数据进行自动转义,相当于使用了一次 addslashes()函数。
get_magic_quotes_gpc(), 返回 0 表示关闭本功能;返回 1 表示本功能打开。
没有set_magic_quotes_gpc() ,不能在代码中动态打开或关闭该配置,只能修改配置文件。
当magic_quotes_gpc关闭时,使用addslashes()函数对字符串进行转义。
++++++++++++++++++++++++++++++++++++++++++++++++++++++
其他大神的经验总结:
一、对于GPC,不管系统有没有开启magic_quotes_gpc(即php.ini中magic_quotes_gpc = On),我们统一开启 magic_quotes_gpc,对get、post、cookie的内容进行转义。操作如下:
(摘自uchome系统)
if (is_array($string)) {
foreach ($string as $key => $val) {
$string[$key] = saddslashes($val);
}
} else {
$string = addslashes($string);
}
return $string;
}
//GPC过滤
$magic_quote = get_magic_quotes_gpc();
if(empty($magic_quote)) {
$_GET = saddslashes($_GET);
$_POST = saddslashes($_POST);
}
//COOKIE,给cookie值转义
$prelength = strlen($_SC['cookiepre']);
foreach ($_COOKIE as $key => $val) {
if(substr($key, 0, $prelength) == $_SC['cookiepre']) {
$_SCOOKIE[(substr($key, $prelength))] = empty($magic_quote) ? saddslashes($val) : $val;
}
}
二、对于magic_quotes_runtime,我们统一关闭它,即set_magic_quotes_runtime(0);不让从数据库读取出来的数据的单引号、双引号和反斜杠都自动被加上\。这样,对数据库的操作如下:添加数据到数据库之前,我们手动对数据进行addslashes(),而从数据库取出数据时,则作相反操作,即stripslashes()。
三、对于要序列化的内容,要保持裸数据,即要去掉转义,stripslashes(),然后在把序列化过的内容保存到数据库当中(注意,序列化过的内容是不带单引号(')、双引号(”)、反斜线(\)的),示例如下:
$feedarr['body_data'] = serialize(stripslashes($body_data));
- magic_quotes_runtime 和 magic_quotes_gpc 的使用
- magic_quotes_gpc和magic_quotes_runtime 的作用
- magic_quotes_gpc, magic_quotes_runtime 和 magic_quotes_sybase
- magic_quotes_gpc和magic_quotes_runtime
- 魔法引用函数magic_quotes_gpc和magic_quotes_runtime的区别和用法
- 魔法引用函数magic_quotes_gpc和magic_quotes_runtime的区别和用法
- [转]magic_quotes_gpc和magic_quotes_runtime的区别和用法
- PHP 中 magic_quotes_gpc 和 magic_quotes_runtime 的区别,作用和用法
- 魔法引用函数magic_quotes_gpc和magic_quotes_runtime的区别和用法
- PHP中magic_quotes_gpc和magic_quotes_runtime的区别、作用和用法
- 魔法引用函数magic_quotes_gpc和magic_quotes_runtime的区别和用法
- PHP中magic_quotes_gpc和magic_quotes_runtime的区别、作用和用法
- PHP中magic_quotes_gpc和magic_quotes_runtime的区别、作用和用法
- PHP中magic_quotes_gpc和magic_quotes_runtime的区别、作用和用法
- magic_quotes_gpc 合magic_quotes_runtime的区别!
- 基于magic_quotes_gpc与magic_quotes_runtime的区别与使用介绍
- PHP魔法引用magic_quotes_gpc和magic_quotes_runtime
- PHP中magic_quotes_gpc和 magic_quotes_runtime区别
- 第三方分享与登录
- HTML总结
- Thinking in Netty
- CSS 图片库
- Machine Learning week1
- magic_quotes_runtime 和 magic_quotes_gpc 的使用
- 我的第一篇博客
- Git教程2——Git 基础
- I2C协议->裸机程序->adapter驱动程序分析
- Jetty 9嵌入式开发
- php总结
- Java 实现日期 Date 的赋值
- CSS
- Exynos4412 所用内存 —— DDR2