网络推荐

推荐给好友 上一篇 | 下一篇

对于magic_quotes_gpc的一点认识

对一般人来说看下前两段就可以了BSD爱好者乐园||8mA6g#J*o

)P tT0]g6oMagic Quotes
ox$UZW.j ~
"L!r^,HBj
CODE:
Magic Quotes is a process that automagically escapes incoming data to the PHP scriptIts preferred to code with magic quotes off and to instead escape the data at runtime, as needed
6nYK/Yl)y Os`Y#~}
BSD爱好者乐园 gY oKwH
What are Magic Quotes
f sxqG'S9]o

0M oda.Xg0d4OC
jp q G1Ne
CODE:
When onall  (single-quote), " (double quote), \ (backslash) and NULL characters are escaped with a backslash automatically. This is identical to what addslashes() does.    
&j3{1q/_'s~.~  BSD爱好者乐园l(Po$e5jqe5ERM
There are three magic quote directives: ;
BSD爱好者乐园k8AxfkSC

4Ne2}N GW.{:fUmagic_quotes_gpc
1b%l)Eh&p5g [Q-W
.zO*H {3a
CODE:
Affects HTTP Request data (GETPOST, and COOKIE). Cannot be set at runtime, and defaults to on in PHP
hi {"t5w(pi`

#s;~1N2XO*clZX @vmagic_quotes_runtime
(Uxu&m3RYwBSD爱好者乐园? ujS8H,CQds
CODE:
If enabledmost functions that return data from an external sourceincluding databases and text fileswill have quotes escaped with a backslashCan be set at runtime, and defaults to off in PHPBSD爱好者乐园,qz}j5Ot;?Ja

U|+ZQ?magic_quotes_sybase
:Bpn!rvTI2A-hBSD爱好者乐园g?$^o'`y1b7i3S'P
CODE:
If enableda single-quote is escaped with a single-quote instead of a backslash. If onit completely overrides magic_quotes_gpcHaving both directives enabled means only single quotes are escaped as ''Double quotesbackslashes and NULLs will remain untouched and unescaped
F&yad&S*F
BSD爱好者乐园R$~YY3R S!@wz[c
Why use Magic QuotesBSD爱好者乐园MQ!k c|/]C

,m"A:h&XO]"l]BSD爱好者乐园V5p+uv k fC M
BSD爱好者乐园9q7Y8dYWr[
BSD爱好者乐园l }(wm I6^D0YZ\'B
1 Useful for beginners     
:s9u/g9d]k   BSD爱好者乐园]W}P3VL7l
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.     
4wO*Z`q4L;D   
A!f9a!\7V2Convenience     BSD爱好者乐园%RsN |}9lJZ.rHW
   
k!b.\:N$K,`For inserting data into a database, magic quotes essentially runs addslashes() on all Get, Post, and Cookie data, and does so automagically.     
E q `#Iq   BSD爱好者乐园zR~.J$vTP'N

Jmjs0DJWhy not to use Magic Quotes
}:e)wuo a6oBSD爱好者乐园p's8W#Udk){f+l
BSD爱好者乐园&yN:_{8uin
BSD爱好者乐园qr~*u(d^ Z.R{D B,x
BSD爱好者乐园^ o R)faCY(if R,o
1 PortabilityBSD爱好者乐园,a"X!V,K a[bS

'[(r#FNK8x
CODE:
Assuming it to be on, or offaffects portability. Use get_magic_quotes_gpc() to check for this, and code accordinglyBSD爱好者乐园` Pk^yK

h n"zk*d'd-\2 PerformanceBSD爱好者乐园0\7J4D!};M,m FQ#r?L#f
BSD爱好者乐园 A1J7P6v;e IEH$Vr
CODE:
Because not every piece of escaped data is inserted into a databasethere is a performance loss for escaping all this dataSimply calling on the escaping functions (like addslashes()) at runtime is more efficient.     
#l%i,Gw%g#I w%u,U   
KZDB|t5ze+i;y"L
Although php.ini-dist enables these directives by default, php.ini-recommended disables itThis recommendation is mainly due to performance reasons
:}d2V-M4u-u)z

c5cV?$qL,@#V|3 InconvenienceBSD爱好者乐园}i{X"}+Q)o8_"y

GO!|5XX i3~.u
CODE:
Because not all data needs escapingit's often annoying to see escaped data where it shouldn't be. For exampleemailing from a form, and seeing a bunch of  within the emailTo fixthis may require excessive use of stripslashes(). BSD爱好者乐园h oW._/B6j_{:D[/v
BSD爱好者乐园!JQU.ddx5_
这些英文实在是需要像我这类人有足够的耐心啊(不是说我有耐心,而是我英语烂),刚才也说了,对于一般人只看下前两段就可以了,特别是我用红色标出来的字!!!
)L-d,uH1a PlE L"hXBSD爱好者乐园/q$pPIE8] B_)G
另外,特别注意的是,魔术引用发生作用是在传递$_GET,$_POST,$_COOKIE时
i0X9O|'oeBSD爱好者乐园\QB9bi-Z
下面是案例
Gg|GOC-rI!abBSD爱好者乐园!F+u9~ Dyn#_
CODE:
1. 
X0}yc8c2he.N条件: magic_quotes_gpc
=off 
%L.O-P:EP;_|写入数据库的字符串未经过任何过滤处理。从数据库读出的字符串也未作任何处理。 
v y*TQ8R]BSD爱好者乐园S|n_ pp)] N?&k
数据: 
 $data="snow''''sun" ; (snow和sun之间是四个连续的单引号). BSD爱好者乐园"f(l/s.D,IY~ ^3U

KE1quO;@d0q}:B3e
操作: 将字符串:"snow''''sun" 写入数据库, BSD爱好者乐园+i.`;L6`M ~%g-G

rpteM ~i结果: 出现sql语句错误,mysql不能顺利完成sql语句,写入数据库失败。 BSD爱好者乐园7giQq-fb[[

:@.[5TXolg.f^数据库保存格式:无数据。 BSD爱好者乐园l,sJ4_"ek{#e7r
BSD爱好者乐园4s_ ]3x;zB0Yp
输出数据格式:无数据。 
2RO [7G9iC N
HU-YDP0P{Av说明: 对于未经处理的单引号在写入数据库时会使sql语句发生错误。 

!XA0D,Nad_
BSD爱好者乐园 \.O}u#IX`
BSD爱好者乐园6^2M|"@aU)}He
BSD爱好者乐园u0`6_'p2}9Fu*V
CODE:
2. BSD爱好者乐园DWw-l'}5U V&J
条件: magic_quotes_gpc
=off 
YS)H%[$Qb(YU[mw写入数据库的字符串经过函数addlashes
()处理。从数据库读出的字符串未作任何处理。 
(^)Y S7?hE
C9A)R }-Q7IRy0w6G数据: 
 $data="snow''''sun" ; (snow和sun之间是四个连续的单引号). BSD爱好者乐园AZl8O0S

CsbT'Q b
操作: 将字符串:"snow''''sun" 写入数据库, BSD爱好者乐园4T5{2o/|8x!H+a%K-R(e
BSD爱好者乐园mV B.e*h;h-I(\
结果: sql语句顺利执行,数据成功写入数据库 BSD爱好者乐园\BL&a`Q

#k3R`.B/e数据库保存格式:snow
''''sun (和输入一样BSD爱好者乐园jQ B Q3fo"R7O'M

|2jT`2l%Z`-^D
输出数据格式:snow''''sun (和输入一样BSD爱好者乐园8n dkKS{

`6e5Jf"k`8Zx*O%y
说明: addslashes()函数将单引号转换为'的转义字符使sql语句成功执行, 
+m6X6Q X,UB&Q\)J但\'并未作为数据存入数据库,数据库保存的是snow'''
sun 而并不是我们想象的snowsun 
'f4bH%k7YA\

+RB&w5FABSD爱好者乐园 F o3c0O k-T/@$Za
BSD爱好者乐园N#yYhq&s;F w D
CODE:
3. BSD爱好者乐园[ldM:L3J
条件: magic_quotes_gpc
=on 
B9tjo#XYN.[z写入数据库的字符串未经过任何处理。从数据库读出的字符串未作任何处理。 
M#o.i P0Q4oFqY
DU$@e5pN3iN:XI`8o数据: 
 $data="snow''''sun" ; (snow和sun之间是四个连续的单引号). BSD爱好者乐园,b1E5wd rv
BSD爱好者乐园7L&QV8ES.s&@
操作: 将字符串:"snow''''sun" 写入数据库, BSD爱好者乐园'z6^9T2cW"Ae

D"dya(c结果: sql语句顺利执行,数据成功写入数据库 
Z"[W9gU'nvLcBSD爱好者乐园3V/`ZT D
数据库保存格式:snow
''''sun (和输入一样
7dgD2u;oBSD爱好者乐园'Us9v2Z] {B/|
输出数据格式:snow''''sun (和输入一样BSD爱好者乐园X }DK[6@zf X*G

4_(S H3kbBe
说明: magic_quotes_gpc=on 将单引号转换为'的转义字符使sql语句成功执行, BSD爱好者乐园g/zm"eb8jg5p
但\'并未作为数据入数据库,数据库保存的是snow'''
sun而并不是我们想象的snowsun。 
ma]j;p3MT

)~L d.?+Pq*MBSD爱好者乐园/L_@[LO%r(h/o6HN
BSD爱好者乐园wSY#[{(k%_:N3b!d
CODE:
4. BSD爱好者乐园f4V-Dr kBD*x5A
条件: magic_quotes_gpc
=on 
B |#@8Vb写入数据库的字符串经过函数addlashes
()处理。从数据库读出的字符串未作任何处理。 BSD爱好者乐园i;nAT:vyFO%Z
BSD爱好者乐园y:zF)Xk!g/huE;F-y
数据: 
 $data="snow''''sun" ; (snow和sun之间是四个连续的单引号). 
+}Z\!I9~j
c%g1cQ)G2H;S
操作: 将字符串:"snow''''sun" 写入数据库, BSD爱好者乐园e/hu'ny
BSD爱好者乐园8fUw|#Ay
结果: sql语句顺利执行,数据成功写入数据库 
-Gsk3x%\*i
.wf3O/tb8m"l{数据库保存格式:snowsun 
(添加了转义字符
KV&hY"^S
:SRw)X4Z^
输出数据格式:snowsun (添加了转义字符BSD爱好者乐园bE/g%V ]#_!D
BSD爱好者乐园q] g ]3`r&r _+I
说明: magic_quotes_gpc=on 将单引号转换为的转义字符使sql语句成功执行, 
%]!Rx KMQ&p&Waddslashes又将即将写入数据库的单引号转换为
,后者的转换被作为数据写入 
xAP'?-YG/h数据库,数据库保存的是snowsun 
BSD爱好者乐园s/xN Bz|[%S)GXa

Fs_.Z(ng%G总结如下:
d'R3c,N;@]
t(C\ x!H'K1. 对于magic_quotes_gpc=on的情况,BSD爱好者乐园ymX(eH\O j

;f"x P$?!@7K我们可以不对输入和输出数据库的字符串数据作BSD爱好者乐园6T1r J#HK$c
addslashes()和stripslashes()的操作,数据也会正常显示。
9?;l9Sj9@6w*P
MHz$c!Xy!T2s-T如果此时你对输入的数据作了addslashes()处理,
3\3Ik a6pb1|)t1a那么在输出的时候就必须使用stripslashes()去掉多余的反斜杠。
(E5u&i!I~\f9iE
f"DOFO1cy2. 对于magic_quotes_gpc=off 的情况
1X M.u#m!m C`g(YBSD爱好者乐园)Q'Y[$uG
必须使用addslashes()对输入数据进行处理,但并不需要使用stripslashes()格式化输出
V0YW w9c"W因为addslashes()并未将反斜杠一起写入数据库,只是帮助mysql完成了sql语句的执行。BSD爱好者乐园`^b d,yh`#Im&x*b
BSD爱好者乐园 ZH!~^y*d
补充:BSD爱好者乐园x/B-Nd)k:I
BSD爱好者乐园9zFL#QoJG,t
magic_quotes_gpc 作用范围是:WEB客户服务端;作用时间:请求开始时,例如当脚本运行时.
4K9Xv(P v,N7^B!Y8[cmagic_quotes_runtime 作用范围:从文件中读取的数据或执行exec()的结果或是从SQL查询中得到的;作用时间:每次当脚本访问运行状态中产生的数据
因为使用范围的限制,BSD方面文章更新速度不快,站长会坚持每天更新博客,欢迎访问!
[版权声明]BSD爱好者乐园站内文章,如来源不是互联网,则均系原创或翻译之作,可随意转载,或以此为基础进行演译,但务必以链接形式注明原始出处和作者信息,否则属于侵权行为。另对本站转载他处文章,俱有说明,如有侵权请联系本人,本人将会在第一时间删除侵权文章。
TAG: magic quotes gpc
 

评分:0

我来说两句

seccode