|
|
前面说了如何阻止 iframe里引用的网页自动跳转,已达到iframe页面的目的。
" P4 | i1 ^/ I8 z, t这次讲一下如何防止自己的网页被别人iframe。
2 q* C7 E! {) L" u" F, E
1 i, \0 m4 Q5 B% L$ N1、这些方法都可行,但不是太可靠。7 v4 ?" ^6 T2 c
- <script language="javascript">$ @4 |0 e$ I, J
- if( top.location != self.location) top.location.href=self.location.href;
3 F9 i/ q5 m0 B- {% t- A - </script>
复制代码 或+ p, K7 v/ y4 V3 h
- <script language="javascript">
- o& M- b2 M4 p5 ]# L - if (top.location != location) top.location.href = location.href;2 b/ ^- ~% E& ^$ R7 \* W
- </script>
复制代码 或0 m: q& P% d: _& W8 f- w2 N* I
- <script language="javascript">
& W$ q, x- P1 a! K4 { w3 e - if (top.location != self.location) {top.location=self.location;}* P3 f! Y, F0 |# W g
- </script>
复制代码 或
/ W& l, H& M% g+ F- r- @# a- <script language="javascript">0 F6 i5 c6 D7 h. r2 i4 C
- if (top.frames.length!=0) top.location=self.document.location;0 @$ n. t8 ^3 F1 S3 D
- </script>
复制代码 不可靠的原因:( {" p/ V8 C, s( C$ S1 Q# w6 y6 K
当别人用如下类似代码做IFRAME嵌套调用时,就可能躲过你的页面的javascript代码。$ _" a+ d) B) @
- <iframe src="http://xp6.org/" name="tv" marginwidth="0" marginheight="0" scrolling="No" noResize frameborder="0" id="tv" framespacing="0" width="580" height="550" VSPACE=-145 HSPACE=-385></iframe>+ l3 o6 w% j$ j# b/ m- t6 C* u% e! m
- <script language="javascript"> ' g9 b$ {$ \8 w d
- var location="";1 F, \6 Y1 y# ~* K: P9 i
- var navigate="";0 R% o8 {: v, |3 P) ]
- frames[0].location.href="";
4 s# ]: G* r& o* _* D - </script>
复制代码 或6 m# n8 o, r0 i8 k. H
- <iframe src="http://xp6.org/" class="t-iframe" scrolling="no" security="restricted" sandbox="">
复制代码 或
: Y3 _5 O8 [4 d+ n8 `2 Q+ v' a: T5 z
- <script type="text/javascript" charset="utf-8">/ `1 w- I3 M, A; o8 U q
- document.write('<iframe seamless sandbox security="restricted" id="url_mainframe" frameborder="0" scrolling="yes" name="main" src="http://xp6.org/" style="height:100%; visibility: inherit; width: 100%; z-index: 1;overflow: visible;"></iframe>');
* i! I j1 k9 K - </script>
复制代码 2、代码层次的屏蔽,方法可行,但不是很人性化。2 V( ^" o: f- l) m: j6 f
Meta标签方法2 n; P7 Q6 Y# o( K8 e
- <meta http-equiv="X-FRAME-OPTIONS" content="DENY">
复制代码 PHP方法4 N6 ]* H( A. [# q* N, J9 u
- <?php header('X-Frame-Options:Deny'); ?>
复制代码 Apache主机方法
+ N" U$ E: d& [ L# u- Header always append X-Frame-Options SAMEORIGIN
复制代码 Nginx主机方法8 D5 G' @7 T& ?0 ~ V
- add_header X-Frame-Options "SAMEORIGIN";
复制代码
* h3 Z% [4 R+ l, v5 ^- }5 c.htaccess方法0 a6 }6 P. I+ s4 @
在网站根目录下的.htaccess文件中中加一句
4 N. X# n/ p r& i0 ]( U* U7 [. a$ ?- Header append X-FRAME-OPTIONS "SAMEORIGIN"
复制代码 J" D5 y0 B8 G% T+ Q/ }% |4 U+ t
IIS方法
7 O! V, N1 F9 L9 G在web.config文件中加- <system.webServer>
9 K$ z y) \3 Z; ]$ p0 z, m7 e - ...: }/ E# E- Q; M5 q* ]" [
- <httpProtocol>
! }& j. F/ K( e' G+ m - <customHeaders>
% v- M2 p' e, b6 \. c' c - <add name="X-Frame-Options" value="SAMEORIGIN" />
8 `# u' |/ o9 F - </customHeaders> * k0 o' L" I7 g' H6 f. x4 ]
- </httpProtocol> - ?7 @2 Y. n$ x* |& ]
- ...; P8 H% O& V! ~
- </system.webServer>
复制代码
w- b5 n. l6 L% Y4 o$ W之所以说不人性化,是因为这样操作之后,别人iframe会显示服务器拒绝之类的信息,虽然是屏蔽了,但是自己网站也不能宣传了。
) w& p+ F: K. \5 p
: [' } B2 [8 n3、推荐解决方法。
: i% N9 l) r0 n* t' y0 L' y- <script language="javascript">
2 y! P0 h1 G. P. {( P$ n; | - if(top != self){
! i, Z0 |1 t0 v; Z - location.href = "http://xp6.org/iframe.html";
, C0 o4 R& i; h/ r9 Y) g4 ? - top.location.href=self.location.href;
/ D: O* f9 n) s; U- T - } & ]! m/ [7 J' z- y% n
- </script>
复制代码 ( |# g. W) i( y2 J5 ]# Q4 v
将上面的代码复制到需要屏蔽页面<head>里面加载。
& W' P0 l' i. @3 v9 U3 }制作一个iframe.html页面里面放张网站宣传图片,加个链接,放到网站根目录。
3 q' d. e2 d2 j原理:8 ] P" q0 B; J
第一步直接强制访问咱自己设定的iframe.html页面,如果网站禁用了JS跳转,那么执行到这一步就结束了,这样即使他们iframe了咱的网站,看到的只是咱的图片宣传页。; U: P- i# k! r5 u1 q9 P; P
第二步,如果没有禁用JS,那么会接着执行top.location.href=self.location.href; ,然后跳转到我们的网址页面。
! P9 Y$ A2 r6 W, x- Y目前测试这种方法没什么问题。
& `- k( i3 n% y* A+ O; E- E5 h
! y4 ^5 p6 P) j0 |+ s( b' u |
|