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

对于magic_quotes_gpc的一点认识

对一般人来说看下前两段就可以了
B0p3[Ai1BSD爱好者乐园_*w ^m5WS9m
Magic Quotes
6B DLk/`1BSD爱好者乐园"\3p$v7? S5N!w
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
M J(C]3y+H Q1
BSD爱好者乐园 U3Ac"V&M.v.c
What are Magic Quotes
(w4O5io[X1

$GEH a Xw1L1Y|7fn1BSD爱好者乐园p]%g5k~Nz+q!`*{
CODE:
When onall  (single-quote), " (double quote), \ (backslash) and NULL characters are escaped with a backslash automatically. This is identical to what addslashes() does.    BSD爱好者乐园H.bV(S~ |w
  BSD爱好者乐园E L.tbO%b:M%av
There are three magic quote directives: ;

-ZJ(u ry'xd1E7lhsO1

tz;EH y"lHH1magic_quotes_gpcBSD爱好者乐园#VN^LiwE
BSD爱好者乐园2Z6OBwUmv
CODE:
Affects HTTP Request data (GETPOST, and COOKIE). Cannot be set at runtime, and defaults to on in PHP
A`G`'ae`q)Fk1
BSD爱好者乐园 LINJ'gz | q ~Se
magic_quotes_runtime
iL2tZx5\/Mi1
(c!S2v*s8Wa1
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爱好者乐园q4S`4ss ~B_-{z
BSD爱好者乐园 P9S3W^@/]
magic_quotes_sybaseBSD爱好者乐园0]FX.tI9E
BSD爱好者乐园UY4M a)z/S)C_&s
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 unescapedBSD爱好者乐园Cb!F6H-A,PB T
BSD爱好者乐园6IC;s3d7vp!r
Why use Magic QuotesBSD爱好者乐园#gZ? t1|,D

l)Y-o!| R `#G#F,U1
T(C*`9N@"Yu1
M!H!H}N{8}+R1
X_y@U"g&i11 Useful for beginners     
y(Xr0^L4I&G@p1   
El ~n%a8mQ V%y1Magic 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.     
7V3hVM|#w q Uvb1   
9u @ sWH12Convenience     BSD爱好者乐园nh\~8D6a8I
   
/{[ D1xSS9K$R1For inserting data into a database, magic quotes essentially runs addslashes() on all Get, Post, and Cookie data, and does so automagically.     BSD爱好者乐园Ip"st#~ Y
   
/n5i*w"I3z7X)x5?*h1
`3|KUG7F+t$\^6A1Why not to use Magic Quotes
a8hv)rQ`1BSD爱好者乐园2@AZ{+wrfl8R

P$|5l:ruX;d1
DMd)K)rgt9tq'g?1BSD爱好者乐园:j$Po-^$\;LB
1 PortabilityBSD爱好者乐园(BN7W5Y,X4J!m]6{

x i r'` Np(J1
CODE:
Assuming it to be on, or offaffects portability. Use get_magic_quotes_gpc() to check for this, and code accordingly
_ O;^+r I3a/x]3u:Ig1
BSD爱好者乐园X:ML6x"X
2 Performance
I.i#]$J[3^/]1BSD爱好者乐园6JR!L$["~0Y Yr
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.     
+}gs+r3L]O|1   
9ee'nwk1
Although php.ini-dist enables these directives by default, php.ini-recommended disables itThis recommendation is mainly due to performance reasons
dV7_9pM1
BSD爱好者乐园;h2q {O1B
3 Inconvenience
q.dj/J;^1
WV0n] vK{ W1
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爱好者乐园Oc*h` uLj

&Q(B"W;E|:I/D#~#B1这些英文实在是需要像我这类人有足够的耐心啊(不是说我有耐心,而是我英语烂),刚才也说了,对于一般人只看下前两段就可以了,特别是我用红色标出来的字!!!
5O%tc!vy0v1BSD爱好者乐园T E)Qo/xjd
另外,特别注意的是,魔术引用发生作用是在传递$_GET,$_POST,$_COOKIE时BSD爱好者乐园+G4Bvs @*a9g

Q+LjUB:i"OJ+y$q1下面是案例BSD爱好者乐园;n,~w&`.O$]

)C-cCeqi B3M\1
CODE:
1. BSD爱好者乐园a4Fy(|nS3v/K"Q {7H
条件: magic_quotes_gpc
=off 
V)Z+]B }sI1写入数据库的字符串未经过任何过滤处理。从数据库读出的字符串也未作任何处理。 BSD爱好者乐园 rIHY-o@&hx

'WX3E.N;O ghVF6Tq8A1数据: 
 $data="snow''''sun" ; (snow和sun之间是四个连续的单引号). BSD爱好者乐园 Ol0{2}S+g+h }(Z%L

%^%OC Xo4I5G1
操作: 将字符串:"snow''''sun" 写入数据库, BSD爱好者乐园0{.OS3F3e VdH

p{}%`0U3r8Y.l'C1结果: 出现sql语句错误,mysql不能顺利完成sql语句,写入数据库失败。 
#u1Cys d z.F,Ho$E1
{ k R6pum8nn1数据库保存格式:无数据。 
jba+H(j}1BSD爱好者乐园J({-W(X.}
输出数据格式:无数据。 BSD爱好者乐园9L"@V0~o2ax
BSD爱好者乐园sr/{#]3C&p8Mr T
说明: 对于未经处理的单引号在写入数据库时会使sql语句发生错误。 

lZi:w6\~/^/x1

z%}*vE c*];Y1
N1Aw$F'tD1
tq"S6_|@n1
CODE:
2. BSD爱好者乐园r(b"kC F!U,T @
条件: magic_quotes_gpc
=off 
uK2I(Do,x2\:~4Q1写入数据库的字符串经过函数addlashes
()处理。从数据库读出的字符串未作任何处理。 BSD爱好者乐园PGj)Kt$^-fQ&H
BSD爱好者乐园?JS7S){{/S6l0nC-\
数据: 
 $data="snow''''sun" ; (snow和sun之间是四个连续的单引号). 
tQoKt.A0}1
%h3N7G~Q7v1
操作: 将字符串:"snow''''sun" 写入数据库, BSD爱好者乐园G D*Y`5f6_d

vPBn.Ae4}1结果: sql语句顺利执行,数据成功写入数据库 
2XfM@_ v+e9mKn1
,Q'\#W haP Q&y1数据库保存格式:snow
''''sun (和输入一样
hR"E-NPYl1
,q8N Zn:W+}1
输出数据格式:snow''''sun (和输入一样BSD爱好者乐园Ji+x0geY

Tr!iPtF|^1
说明: addslashes()函数将单引号转换为'的转义字符使sql语句成功执行, 
1EJ4r8sm+LI]]1但\'并未作为数据存入数据库,数据库保存的是snow'''
sun 而并不是我们想象的snowsun 
)Zz.w.q%q#do3x1
BSD爱好者乐园M9A:G{#w ^'K

uMZl3c1BSD爱好者乐园3ly;dkt'?[
CODE:
3. 
L)Wie(ZM ZK!o1条件: magic_quotes_gpc
=on 
xFJ9Cur1写入数据库的字符串未经过任何处理。从数据库读出的字符串未作任何处理。 BSD爱好者乐园5i0@t.BZ'_3N
BSD爱好者乐园S7h!f9jm_
数据: 
 $data="snow''''sun" ; (snow和sun之间是四个连续的单引号). 
PA#z#n%w,y_'E1
)tD#v!gqiP ^,B1
操作: 将字符串:"snow''''sun" 写入数据库, BSD爱好者乐园+BU/O8g P'Z#^$E

J}(N+l T6xz1结果: sql语句顺利执行,数据成功写入数据库 
G4w u_ z1j1
3f K qHn7s1数据库保存格式:snow
''''sun (和输入一样BSD爱好者乐园8h-ig2mU

3nky `p$aR1
输出数据格式:snow''''sun (和输入一样BSD爱好者乐园N/`~ T7\%yJ
BSD爱好者乐园4hQ*K2O"K-k
说明: magic_quotes_gpc=on 将单引号转换为'的转义字符使sql语句成功执行, 
x-q_I k1但\'并未作为数据入数据库,数据库保存的是snow'''
sun而并不是我们想象的snowsun。 
$r:q k P+`1

8E#EB#Ia\X%V)qP'\A3O1
Hv3H)U ^!n1BSD爱好者乐园&Y!S~ K)B
CODE:
4. BSD爱好者乐园ZV(i o;cxE
条件: magic_quotes_gpc
=on BSD爱好者乐园5H%XB2u'J;|
写入数据库的字符串经过函数addlashes
()处理。从数据库读出的字符串未作任何处理。 
u.X[%Y%s ]o6A1
,t*\-F~yh1数据: 
 $data="snow''''sun" ; (snow和sun之间是四个连续的单引号). BSD爱好者乐园2|Enp.z
BSD爱好者乐园!A%B-r{SN
操作: 将字符串:"snow''''sun" 写入数据库, BSD爱好者乐园S"w"j Gn-u(k z

$K:h6Y.v@M7a\#b1结果: sql语句顺利执行,数据成功写入数据库 
8g`v2Db1BSD爱好者乐园$L:L@"O.S p Y&p O8f
数据库保存格式:snowsun 
(添加了转义字符
}'v$Z7Y?0iZ1
Ythd.t @)U|1
输出数据格式:snowsun (添加了转义字符BSD爱好者乐园*J+Y*nu!CV"^

#E GF6M%t,ti1
说明: magic_quotes_gpc=on 将单引号转换为的转义字符使sql语句成功执行, BSD爱好者乐园1Y u,q*fI(D+Y4O_!y
addslashes又将即将写入数据库的单引号转换为
,后者的转换被作为数据写入 BSD爱好者乐园m+lR]/`9v9um,Q
数据库,数据库保存的是snowsun 

"[6r*JlG:d T1

nj0rb.K kP1总结如下:BSD爱好者乐园T/N4o1GK n&}ul

%e"{EB'{,LH11. 对于magic_quotes_gpc=on的情况,
mFr&j0x-z1
Q/O#n0tF!g1我们可以不对输入和输出数据库的字符串数据作
-L;{8V K;O G)n1addslashes()和stripslashes()的操作,数据也会正常显示。
5@'|d7p$jE1BSD爱好者乐园X9yA)M9Xe4o'p!~Z
如果此时你对输入的数据作了addslashes()处理,BSD爱好者乐园 h)klyj
那么在输出的时候就必须使用stripslashes()去掉多余的反斜杠。
"z\K1va1BSD爱好者乐园&G TEDO4?b*zC
2. 对于magic_quotes_gpc=off 的情况
.}E(V_G0D"|c0Mc1BSD爱好者乐园A@&Y$U Pjy*k%]Ly
必须使用addslashes()对输入数据进行处理,但并不需要使用stripslashes()格式化输出
*k0uPO?lo~ Q1因为addslashes()并未将反斜杠一起写入数据库,只是帮助mysql完成了sql语句的执行。BSD爱好者乐园;Z qz1EOK-Oa

M!H"Gy g_+t1补充:BSD爱好者乐园t0}y L a |I
BSD爱好者乐园 b*e?\qF*EXuo$~
magic_quotes_gpc 作用范围是:WEB客户服务端;作用时间:请求开始时,例如当脚本运行时.BSD爱好者乐园(KY+_d)Q_3L
magic_quotes_runtime 作用范围:从文件中读取的数据或执行exec()的结果或是从SQL查询中得到的;作用时间:每次当脚本访问运行状态中产生的数据

[重要提醒]对本篇资料有疑问,请到论坛讨论,尽量使文章准确无误>>>
[版权声明]BSD爱好者乐园站内文章,如来源不是互联网,则均系原创或翻译之作,可随意转载,或以此为基础进行演译,但务必以链接形式注明原始出处和作者信息,否则属于侵权行为。另对本站转载他处文章,俱有说明,如有侵权请联系本人,本人将会在第一时间删除侵权文章。
TAG: magic quotes gpc
 

评分:0

我来说两句

seccode