php中magic_quotes_gpc函数一些用法_PHP教程

php教程评论8.1K views阅读模式

magic_quotes_gpc方法是根据你php.ini配置来的,如果打开了magic_quotes_gpc就生成,他的作用与addslashes是一样的,下面我来详细介绍一下关于magic_quotes_gpc用法。

看了thinksaas部分源码,发现对$_POST/$_GET过来的数据处理方法是通过函数Add_S()进行的,即环境默认没有开启magic_quotes_gpc,就对提交过来的数据进行addslashes()处理。

一直对magic_quotes_gpc感到疑惑,前面我也有发过对magic_quotes_gpc的文章《magic_quotes_gpc和addslashes()的正确关系?》,现在再谈这个问题,就是想彻底搞明白这个东西,我已经在thinksaas官网提交了这个问题,等候答复,到时我把结果更新到本文。
问题1:现在要读取数据里的数据是不是读取后要进行stripslashes()处理,才能还原到原来的本来的数据状态?

问题2:我看很多其他程序都是反过来处理的,即如果环境开启magic_quotes_gpc了,就对提交过来的数据进行stripslashes()处理,然后再对数据进行htmlspecialchars()处理去替换掉那些特殊符号,我想问这种方法和thinksaas的处理方法哪种好?听说magic_quotes_gpc以后默认是不开启的。

typecho火车头发布接口,我处理post过来的数据就是采用问题2中的方法,不知道是不是最好的方法?

对提交过来的数据进行stripslashes()处理,然后再对数据进行htmlspecialchars()--这种方法我想没有啥优点吧。相比还是TS的好。如果特别点的网站,比如微博之类格式很少的,我看只须addslashed()就行,然后直接入库最好。
问题1没有人回答,不过我在这里可以自己回答,无论开没开magic_quotes_gpc,读取数据后都不需要再进行stripslashes()处理,因为保存的时候数据并没有加上额外的反斜线。

magic_quotes_gpc总结

1、处理方法

方法一:如果系统环境没有开启magic_quotes_gpc,就对提交过来的数据进行addslashes()处理。
方法二:如果系统环境开启magic_quotes_gpc,就对提交过来的数据进行stripslashes()处理,最后再对数据进行htmlspecialchars()处理去掉那些特殊符号。

2、最好的方法如那个兄弟说的一样,简单的入库就直接addslashed()后入库就可以了;如果需要对字符串进行比较复杂的处理后再入库,一般需要先去掉magic_quotes_gpc自动添加的反斜线,然后进行字符串的处理,处理完后再addslashed()或者htmlspecialchars()处理,最后入库。虽然一般是这样,但是还是要根据实际灵活采取方法的。

2012-10-21日更新

最最好的方法是:去掉magic_quotes_gpc自动添加的反斜线,然后在数据库操作类里把所有入库的操作都先addslashed(),再入库

现在看看官方操作怎么说的

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

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

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_magic_quotes_gpc

取得 PHP 环境变数 magic_quotes_gpc 的值。

语法: long get_magic_quotes_gpc(void);

传回值: 长整数

函式种类: PHP 系统功能

内容说明

本函式取得 PHP 环境设定的变数 magic_quotes_gpc (GPC, Get/Post/Cookie) 值。传回 0 表示关闭本功能;传回 1 表示本功能开启。当 magic_quotes_gpc 开启时,所有的 ' (单引号), " (双引号), '' (反斜线) and 空字符会自动转为含有反斜线的溢出字符。

addslashes -- 使用反斜线引用字符串

描述
string addslashes ( string str)

返回字符串,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线。这些字符是单引号(')、双引号(")、反斜线('')与 NUL(NULL 字符)。

一个使用 addslashes() 的例子是当你要往数据库中输入数据时。例如,将名字 O'reilly 插入到数据库中,这就需要对其进行转义。大多数据库使用 '' 作为转义符:O'''reilly。这样可以将数据放入数据库中,而不会插入额外的 ''。当 PHP 指令 magic_quotes_sybase 被设置成 on 时,意味着插入 ' 时将使用 ' 进行转义。

默认情况下,PHP 指令 magic_quotes_gpc 为 on,它主要是对所有的 GET、POST 和 COOKIE 数据自动运行 addslashes()。不要对已经被 magic_quotes_gpc 转义过的字符串使用 addslashes(),因为这样会导致双层转义。遇到这种情况时可以使用函数 get_magic_quotes_gpc() 进行检测。

例子 1. addslashes() 示例

代码如下 复制代码

$str = "Is your name O'reilly?";

// 输出:Is your name O'''reilly?
echo addslashes($str);
?>

get_magic_quotes_gpc()
本函数取得 PHP 环境配置的变量 magic_quotes_gpc (GPC, Get/Post/Cookie) 值。返回 0 表示关闭本功能;返回 1 表示本功能打开。当 magic_quotes_gpc 打开时,所有的 ' (单引号), " (双引号), '' (反斜线) and 空字符会自动转为含有反斜线的溢出字符。

代码如下 复制代码

function html($str) {
$str = get_magic_quotes_gpc()?$str:addslashes($str);
return $str;
}

总结如下:

1. 对于PHP magic_quotes_gpc=on的情况,

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

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

2. 对于PHP magic_quotes_gpc=off 的情况

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

企鹅博客
  • 本文由 发表于 2020年8月12日 08:42:41
  • 转载请务必保留本文链接:https://www.qieseo.com/307222.html

发表评论