来自 计算机教程 2019-08-26 14:02 的文章
当前位置: 美洲杯冠军竞猜 > 计算机教程 > 正文

php magic_quotes_gpc的一点认识与分析

输出数据格式:无数据。

对于php中magic_quotes_gpc的一点认识

作者:blankyao 发布时间:October 9, 2007 分类:PHP笔记

最近一直在做一个文章发布系统,做了改,改了做,一直到现在还没竣工....

 

为了达到更好的兼容性,其中的程序涉及到了magic_quotes_gpc,看了下手册,又找了些资料,分析了下,分享给大家。

 

 

 

----------------------------------DESCRIPTION-------------------------------------------------------

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

 

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

 

Magic Quotes

 

 

PHP代码

  1. 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

 

 

 

PHP代码

  1. When on, all ' (single-quote), " (double quote), / (backslash) and NULL characters are escaped with a backslash automatically. This is identical to what addslashes() does.
  2. There are three magic quote directives:
  3. magic_quotes_gpc
  4. Affects HTTP Request data (GET, POST, and COOKIE). Cannot be set at runtime, and defaults to on in PHP.
    1. magic_quotes_runtime
  5. 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.
  6. magic_quotes_sybase
  7. 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

 

 

 

PHP代码

  1. Useful for beginners
  2. 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.
  3. Convenience
  4. 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

 

 

 

PHP代码

  1. Portability
  2. Assuming it to be on, or off, affects portability. Use get_magic_quotes_gpc() to check for this, and code accordingly.
  3. Performance
  4. 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.
  5. Although php.ini-dist enables these directives by default, php.ini-recommended disables it. This recommendation is mainly due to performance reasons.
  6. Inconvenience
  7. 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时

 

下面是案例

 

条件: magic_quotes_gpc=off

写入数据库的字符串未经过任何过滤处理。从数据库读出的字符串也未作任何处理。

 

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

 

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

 

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

 

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

 

输出数据格式:无数据。

 

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

 

条件: magic_quotes_gpc=off

写入数据库的字符串经过函数addlashes()处理。从数据库读出的字符串未作任何处理。

 

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

 

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

 

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

 

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

 

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

 

说明: addslashes()函数将单引号转换为/'的转义字符使sql语句成功执行,

但/'并未作为数据存入数据库,数据库保存的是snow''''sun 而并不是我们想象的snow/'/'/'/'sun

 

条件: 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。

 

条件: 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()去掉多余的反斜杠。

 

  1. 对于magic_quotes_gpc=off 的情况

 

必须使用addslashes()对输入数据进行处理,但并不需要使用stripslashes()格式化输出

因为addslashes()并未将反斜杠一起写入数据库,只是帮助mysql完成了sql语句的执行。

 

补充:

 

magic_quotes_gpc 作用范围是:WEB客户服务端;作用时间:请求开始时,例如当脚本运行时.

magic_quotes_runtime 作用范围:从文件中读取的数据或执行exec()的结果或是从SQL查询中得到的;作用时间:每次当脚本访问运行状态中产生的数据

 

原帖地址http://www.phpfans.net/bbs/viewthread.php?tid=6860&page=1&extra=page=1

 

标签: php , magic_quotes_gpc

 


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

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

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

代码:
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

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

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

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

篮球世界杯冠军竞猜,补充:

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

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

代码:
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

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

下面是案例

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

代码:

代码:
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. 条件: magic_quotes_gpc=off
    写入数据库的字符串经过函数addslashes()处理。从数据库读出的字符串未作任何处理。

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

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

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

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

1 Useful for beginners

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

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

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

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

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

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

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.

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

本文由美洲杯冠军竞猜发布于计算机教程,转载请注明出处:php magic_quotes_gpc的一点认识与分析

关键词: