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? |
get_magic_quotes_gpc()
本函数取得 PHP 环境配置的变量 magic_quotes_gpc (GPC, Get/Post/Cookie) 值。返回 0 表示关闭本功能;返回 1 表示本功能打开。当 magic_quotes_gpc 打开时,所有的 ' (单引号), " (双引号), '' (反斜线) and 空字符会自动转为含有反斜线的溢出字符。
代码如下 | 复制代码 |
function html($str) { |
总结如下:
1. 对于PHP magic_quotes_gpc=on的情况,
我们可以不对输入和输出数据库的字符串数据作
addslashes()和stripslashes()的操作,数据也会正常显示。
如果此时你对输入的数据作了addslashes()处理,
那么在输出的时候就必须使用stripslashes()去掉多余的反斜杠。
2. 对于PHP magic_quotes_gpc=off 的情况
必须使用addslashes()对输入数据进行处理,但并不需要使用stripslashes()格式化输出
因为addslashes()并未将反斜杠一起写入数据库,只是帮助mysql完成了sql语句的执行。

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3汉化版
中文版,非常好用

记事本++7.3.1
好用且免费的代码编辑器

Dreamweaver Mac版
视觉化网页开发工具