php magic_quotes_gpc的一点认识与分析


Posted in PHP onAugust 18, 2008

blankyao 说“学习的过程就是不断的发现错误,不断的改正错误”;

先看下手册上怎么说的吧!

对一般人来说看下前两段就可以了

Magic Quotes

代码:
Magic Quotes is a process that automagically escapes incoming data to the PHP script. It's preferred to code with magic quotes off and to instead escape the data at runtime, as needed.
What are Magic Quotes

代码:
When on, all ' (single-quote), " (double quote), \ (backslash) and NULL characters are escaped with a backslash automatically. This is identical to what addslashes() does.

There are three magic quote directives:
magic_quotes_gpc

代码:
Affects HTTP Request data (GET, POST, and COOKIE). Cannot be set at runtime, and defaults to on in PHP.
magic_quotes_runtime

代码:
If enabled, most functions that return data from an external source, including databases and text files, will have quotes escaped with a backslash. Can be set at runtime, and defaults to off in PHP.
magic_quotes_sybase

代码:
If enabled, a single-quote is escaped with a single-quote instead of a backslash. If on, it completely overrides magic_quotes_gpc. Having both directives enabled means only single quotes are escaped as ''. Double quotes, backslashes and NULL's will remain untouched and unescaped.
Why use Magic Quotes

1 Useful for beginners

Magic quotes are implemented in PHP to help code written by beginners from being dangerous. Although SQL Injection is still possible with magic quotes on, the risk is reduced.

2Convenience

For inserting data into a database, magic quotes essentially runs addslashes() on all Get, Post, and Cookie data, and does so automagically.

Why not to use Magic Quotes

1 Portability

代码:
Assuming it to be on, or off, affects portability. Use get_magic_quotes_gpc() to check for this, and code accordingly.
2 Performance

代码:
Because not every piece of escaped data is inserted into a database, there is a performance loss for escaping all this data. Simply calling on the escaping functions (like addslashes()) at runtime is more efficient.

Although php.ini-dist enables these directives by default, php.ini-recommended disables it. This recommendation is mainly due to performance reasons.
3 Inconvenience

代码:
Because not all data needs escaping, it's often annoying to see escaped data where it shouldn't be. For example, emailing from a form, and seeing a bunch of \' within the email. To fix, this may require excessive use of stripslashes().
这些英文实在是需要像我这类人有足够的耐心啊(不是说我有耐心,而是我英语烂),刚才也说了,对于一般人只看下前两段就可以了,特别是我用红色标出来的字!!!

另外,特别注意的是,魔术引用发生作用是在传递$_GET,$_POST,$_COOKIE时

下面是案例

代码:
1.
条件: magic_quotes_gpc=off
写入数据库的字符串未经过任何过滤处理。从数据库读出的字符串也未作任何处理。

数据:  $data="snow''''sun" ; (snow和sun之间是四个连续的单引号).

操作: 将字符串:"snow''''sun" 写入数据库,

结果: 出现sql语句错误,mysql不能顺利完成sql语句,写入数据库失败。

数据库保存格式:无数据。

输出数据格式:无数据。

说明: 对于未经处理的单引号在写入数据库时会使sql语句发生错误。

代码:
2.
条件: magic_quotes_gpc=off
写入数据库的字符串经过函数addslashes()处理。从数据库读出的字符串未作任何处理。

数据:  $data="snow''''sun" ; (snow和sun之间是四个连续的单引号).

操作: 将字符串:"snow''''sun" 写入数据库,

结果: sql语句顺利执行,数据成功写入数据库

数据库保存格式:snow''''sun (和输入一样)

输出数据格式:snow''''sun (和输入一样)

说明: addslashes()函数将单引号转换为\'的转义字符使sql语句成功执行,
但\'并未作为数据存入数据库,数据库保存的是snow''''sun 而并不是我们想象的snow\'\'\'\'sun

代码:
3.
条件: magic_quotes_gpc=on
写入数据库的字符串未经过任何处理。从数据库读出的字符串未作任何处理。

数据:  $data="snow''''sun" ; (snow和sun之间是四个连续的单引号).

操作: 将字符串:"snow''''sun" 写入数据库,

结果: sql语句顺利执行,数据成功写入数据库

数据库保存格式:snow''''sun (和输入一样)

输出数据格式:snow''''sun (和输入一样)

说明: magic_quotes_gpc=on 将单引号转换为\'的转义字符使sql语句成功执行,
但\'并未作为数据入数据库,数据库保存的是snow''''sun而并不是我们想象的snow\'\'\'\'sun。

代码:
4.
条件: magic_quotes_gpc=on
写入数据库的字符串经过函数addlashes()处理。从数据库读出的字符串未作任何处理。

数据:  $data="snow''''sun" ; (snow和sun之间是四个连续的单引号).

操作: 将字符串:"snow''''sun" 写入数据库,

结果: sql语句顺利执行,数据成功写入数据库

数据库保存格式:snow\'\'\'\'sun (添加了转义字符)

输出数据格式:snow\'\'\'\'sun (添加了转义字符)

说明: magic_quotes_gpc=on 将单引号转换为\'的转义字符使sql语句成功执行,
addslashes又将即将写入数据库的单引号转换为\',后者的转换被作为数据写入
数据库,数据库保存的是snow\'\'\'\'sun
总结如下:

1. 对于magic_quotes_gpc=on的情况,

我们可以不对输入和输出数据库的字符串数据作
addslashes()和stripslashes()的操作,数据也会正常显示。

如果此时你对输入的数据作了addslashes()处理,
那么在输出的时候就必须使用stripslashes()去掉多余的反斜杠。

2. 对于magic_quotes_gpc=off 的情况

必须使用addslashes()对输入数据进行处理,但并不需要使用stripslashes()格式化输出
因为addslashes()并未将反斜杠一起写入数据库,只是帮助mysql完成了sql语句的执行。

补充:

magic_quotes_gpc 作用范围是:WEB客户服务端;作用时间:请求开始时,例如当脚本运行时.
magic_quotes_runtime 作用范围:从文件中读取的数据或执行exec()的结果或是从SQL查询中得到的;作用时间:每次当脚本访问运行状态中产生的数据

PHP 相关文章推荐
转换中文日期的PHP程序
Oct 09 PHP
php执行sql语句的写法
Mar 10 PHP
php 智能404跳转代码,适合换域名没改变目录的网站
Jun 04 PHP
字母顺序颠倒而单词顺序不变的php代码
Aug 08 PHP
PHP session有效期session.gc_maxlifetime
Apr 20 PHP
解析php安全性问题中的:Null 字符问题
Jun 21 PHP
php使用PDO操作MySQL数据库实例
Dec 30 PHP
php+mysql实现简单的增删改查功能
Jul 13 PHP
PHP实现简单数字分页效果
Jul 26 PHP
php生成gif动画的方法
Nov 05 PHP
Linux下从零开始安装配置Nginx服务器+PHP开发环境
Dec 21 PHP
php简单实现数组分页的方法
Apr 30 PHP
php数组应用之比较两个时间的相减排序
Aug 18 #PHP
php中的数组操作函数整理
Aug 18 #PHP
PHP去除数组中重复的元素并按键名排序函数
Aug 18 #PHP
删除数组元素实用的PHP数组函数
Aug 18 #PHP
PHP 数组实例说明
Aug 18 #PHP
PHP获取网站域名和地址的代码
Aug 17 #PHP
php二分法在IP地址查询中的应用
Aug 12 #PHP
You might like
表单复选框向PHP传输数据的代码
2007/11/13 PHP
PHP HTML JavaScript MySQL代码如何互相传值的方法分享
2012/09/30 PHP
深入探讨:Nginx 502 Bad Gateway错误的解决方法
2013/06/03 PHP
php解析url的三个示例
2014/01/20 PHP
用 Composer构建自己的 PHP 框架之设计 MVC
2014/10/30 PHP
php+xml实现在线英文词典查询的方法
2015/01/23 PHP
PHP中把对象数组转换成普通数组的方法
2015/07/10 PHP
详解WordPress开发中过滤属性以及Sql语句的函数使用
2015/12/25 PHP
php array_walk 对数组中的每个元素应用用户自定义函数详解
2016/11/18 PHP
用roll.js实现的图片自动滚动+鼠标触动的特效
2007/03/18 Javascript
js中判断Object、Array、Function等引用类型对象是否相等
2012/08/29 Javascript
js实现的简单radio背景颜色选择器代码
2015/08/18 Javascript
JavaScript中获取纯正的undefined的方法
2016/03/06 Javascript
jQuery 实现评论等级好评差评特效
2016/05/06 Javascript
JavaScript正则表达式校验与递归函数实际应用实例解析
2017/08/04 Javascript
jQuery实现鼠标响应式透明度渐变动画效果示例
2018/02/13 jQuery
Spring Boot/VUE中路由传递参数的实现代码
2018/03/02 Javascript
vue下history模式刷新后404错误解决方法
2018/08/18 Javascript
vue-router命名路由和编程式路由传参讲解
2019/01/19 Javascript
微信小程序遍历Echarts图表实现多个饼图
2019/04/25 Javascript
JavaScript使用prototype属性实现继承操作示例
2020/05/22 Javascript
[01:39]2014DOTA2国际邀请赛 Newbee经理CU专访队伍火力全开
2014/07/15 DOTA
python分析网页上所有超链接的方法
2015/05/08 Python
Python生成器定义与简单用法实例分析
2018/04/30 Python
详解分布式任务队列Celery使用说明
2018/11/29 Python
CSS3 简写animation
2012/05/10 HTML / CSS
波兰香水和化妆品购物网站:Notino.pl
2017/11/07 全球购物
军训自我鉴定范文
2014/02/13 职场文书
党员公开承诺书范文
2014/03/25 职场文书
中餐厅经理岗位职责
2014/04/11 职场文书
五好关工委申报材料
2014/05/31 职场文书
赢在中国观后感
2015/06/02 职场文书
师范生教育见习总结
2015/06/23 职场文书
Nginx + consul + upsync 完成动态负载均衡的方法详解
2021/03/31 Servers
MySQL利用UNION连接2个查询排序失效详解
2021/11/20 MySQL
HTML中的表格元素介绍
2022/02/28 HTML / CSS