|
|
前面说了如何阻止 iframe里引用的网页自动跳转,已达到iframe页面的目的。
: w! T! m/ h; V6 @# T+ a这次讲一下如何防止自己的网页被别人iframe。
C2 o- B; ~, t2 L5 ~$ g
% }" e9 W. w& | `' J. p1、这些方法都可行,但不是太可靠。6 c- |6 \: U$ @- { s/ h
- <script language="javascript">
$ X# A& W; @# ^1 Y - if( top.location != self.location) top.location.href=self.location.href;
' P0 N; E9 t6 o6 I( n4 d, K3 B - </script>
复制代码 或) `5 l$ F3 E6 _5 i: ?! H3 m4 E1 I
- <script language="javascript">
2 p, y+ w9 }6 { g - if (top.location != location) top.location.href = location.href;* w7 g6 g$ {) I @1 h9 z2 f* q. a8 G
- </script>
复制代码 或
: Y/ ?" o& O6 ~- e3 b- <script language="javascript">5 W( W6 a$ R+ R/ N! I
- if (top.location != self.location) {top.location=self.location;}9 ^2 f9 R. X2 p8 a+ D) G
- </script>
复制代码 或7 ~. v3 E3 W5 R4 Z( ?, n! c: `
- <script language="javascript">
% `, n& x4 h( Y4 [1 P) T! e0 w - if (top.frames.length!=0) top.location=self.document.location;+ }/ q6 Z% _) Q/ i$ x, k
- </script>
复制代码 不可靠的原因:. U) f9 e) e4 ~' g5 B) i; {% |
当别人用如下类似代码做IFRAME嵌套调用时,就可能躲过你的页面的javascript代码。
1 Q( ~) E2 B) F+ f- <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>1 `+ Y; [. B/ v( ?
- <script language="javascript"> 8 b9 I, V7 }0 L4 L) @
- var location="";
! V7 I% n+ Q7 `1 Z - var navigate="";
/ R# r! B7 g l/ R2 X5 P) v+ q - frames[0].location.href="";
5 t- o0 x6 B' Y O: _- F - </script>
复制代码 或8 H8 G4 V D9 K
- <iframe src="http://xp6.org/" class="t-iframe" scrolling="no" security="restricted" sandbox="">
复制代码 或+ w& i7 d' _0 d) J0 x1 o, T# y
& V4 c* T" K8 L j W% s- <script type="text/javascript" charset="utf-8">
) w+ n6 ~. ? m' o! G - 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>');' O& J- t6 G0 g* O
- </script>
复制代码 2、代码层次的屏蔽,方法可行,但不是很人性化。; n- I5 V$ D) v% Y, r
Meta标签方法' t- Y* O. V& N
- <meta http-equiv="X-FRAME-OPTIONS" content="DENY">
复制代码 PHP方法
& L; v$ W0 P* c' @: J! \$ p- <?php header('X-Frame-Options:Deny'); ?>
复制代码 Apache主机方法# }# p4 e' _% E: n- _( L
- Header always append X-Frame-Options SAMEORIGIN
复制代码 Nginx主机方法7 n1 y6 i( J, p
- add_header X-Frame-Options "SAMEORIGIN";
复制代码 ' o$ T+ w) [7 A6 Y( m/ _9 i
.htaccess方法
5 w2 B0 R& [$ s5 {9 c# z在网站根目录下的.htaccess文件中中加一句
! X* h* v; ?) ^: Z/ F) Y- Header append X-FRAME-OPTIONS "SAMEORIGIN"
复制代码
% Q2 `/ I; c/ x/ X; qIIS方法
! t( ?& n7 h* B在web.config文件中加- <system.webServer>
. e% _* F; P9 q6 ]$ b - ...
* o: m8 J; e8 t8 O4 j) g - <httpProtocol>; s" \2 J7 S% j; E+ Y
- <customHeaders># O. d8 `% P' ~ Z
- <add name="X-Frame-Options" value="SAMEORIGIN" />
7 h4 Z% D+ j' O - </customHeaders> / D1 x) O6 ]7 A L" W8 k& v
- </httpProtocol>
% X% w1 w8 w4 b - ...' h- o8 b$ A1 B5 u
- </system.webServer>
复制代码 - W7 ]5 P# C5 I2 u* m1 I: s
之所以说不人性化,是因为这样操作之后,别人iframe会显示服务器拒绝之类的信息,虽然是屏蔽了,但是自己网站也不能宣传了。
|5 c4 C* {' G( }; f" ?) O3 C8 @5 S0 y/ S# X: X" c
3、推荐解决方法。
) }) ?7 d0 x5 f5 o- <script language="javascript"> 3 Z5 q( A5 t! f) k G
- if(top != self){
7 N) [* L1 P5 P8 o3 a2 i - location.href = "http://xp6.org/iframe.html";
/ ^' ^- ?8 A2 x" _ - top.location.href=self.location.href;
; H% l# t, o4 ~* h- n' g @& v - }
q" ^1 t7 ]* V2 d) j' Z, }+ C5 T - </script>
复制代码 7 G/ L! ~# Q; E% Q
将上面的代码复制到需要屏蔽页面<head>里面加载。. i/ b6 W. c- z5 S4 }* z
制作一个iframe.html页面里面放张网站宣传图片,加个链接,放到网站根目录。
. K, C* d, u& h, M/ m2 A& p原理:8 w* M/ I5 u% y, w
第一步直接强制访问咱自己设定的iframe.html页面,如果网站禁用了JS跳转,那么执行到这一步就结束了,这样即使他们iframe了咱的网站,看到的只是咱的图片宣传页。( p) C# z1 c) V; j
第二步,如果没有禁用JS,那么会接着执行top.location.href=self.location.href; ,然后跳转到我们的网址页面。! g! k B d2 K g
目前测试这种方法没什么问题。
% {% I' Z9 T8 e$ l
[5 |+ M8 U- ` |
|