|
|
前面说了如何阻止 iframe里引用的网页自动跳转,已达到iframe页面的目的。
$ b" ]; v/ J1 X这次讲一下如何防止自己的网页被别人iframe。. m: N6 q: ^% T9 W6 m, H
b& K5 b: V& v; f
1、这些方法都可行,但不是太可靠。/ a+ C/ @: P- M, A$ v3 N
- <script language="javascript">
5 H$ ^& k' L' K8 ~: i1 b - if( top.location != self.location) top.location.href=self.location.href;7 r' r" m" b7 P5 V2 W/ ~
- </script>
复制代码 或- a2 q1 W2 [; v4 V8 D
- <script language="javascript"> ^5 V: O- _3 T Q7 V \) a! p
- if (top.location != location) top.location.href = location.href;- N5 l4 Y! W6 _$ k4 u! a* `3 B3 }
- </script>
复制代码 或+ M) s; T- H) {; l9 f, R- h5 h
- <script language="javascript">6 C" N8 R! l$ F: U% _' N
- if (top.location != self.location) {top.location=self.location;}
$ d0 G7 l9 g, @8 K3 _- J - </script>
复制代码 或" ^3 s5 }* A, _5 R* c
- <script language="javascript">. o! @/ l8 b9 s% T$ w& F' D& U8 j. I
- if (top.frames.length!=0) top.location=self.document.location;
7 ^6 `$ J! l, c7 H1 f - </script>
复制代码 不可靠的原因:/ N- m/ q, I3 G6 Y" V
当别人用如下类似代码做IFRAME嵌套调用时,就可能躲过你的页面的javascript代码。: M6 X' c W& s+ \7 m! s# c
- <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>
; Q# O# ?- i4 r6 F& r - <script language="javascript">
; P5 ]* c3 |# O' k4 i2 r7 v, e# @- l - var location="";- U" Z0 h7 ~( _, o) D+ t
- var navigate="";/ r" a+ ?! f$ [- ~3 |$ M) t
- frames[0].location.href="";
! J; v. w& c V6 r. l* S2 \4 t - </script>
复制代码 或( e' U7 J/ n' F. k& \0 M1 x7 c
- <iframe src="http://xp6.org/" class="t-iframe" scrolling="no" security="restricted" sandbox="">
复制代码 或
1 i' q+ l0 |6 C' q+ @6 k" O6 D/ f) }# M; _# ^4 W6 G: Y
- <script type="text/javascript" charset="utf-8">5 e+ D) @$ ~5 X( L; m6 |7 W( D
- 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>');
7 h( {6 j3 d# y - </script>
复制代码 2、代码层次的屏蔽,方法可行,但不是很人性化。8 g6 ~. V U' Y# J5 o6 D6 y* k
Meta标签方法/ z8 c; |5 E4 f( w
- <meta http-equiv="X-FRAME-OPTIONS" content="DENY">
复制代码 PHP方法
( A0 R, M$ A& S5 t3 ^- <?php header('X-Frame-Options:Deny'); ?>
复制代码 Apache主机方法
! A5 h6 g. r, X4 [; l7 e- Header always append X-Frame-Options SAMEORIGIN
复制代码 Nginx主机方法
) S4 P4 N, D7 [, o- H- add_header X-Frame-Options "SAMEORIGIN";
复制代码 & e$ U0 M" B$ |
.htaccess方法' P& B8 X) X: |; ^7 s/ V
在网站根目录下的.htaccess文件中中加一句
% ]" J& [/ q, {( d: n! Z' J: o7 S5 M- l$ m- Header append X-FRAME-OPTIONS "SAMEORIGIN"
复制代码 3 }6 |; [$ X. _" v0 x r8 H+ C! z/ r
IIS方法, F# f$ U2 N+ c# r
在web.config文件中加- <system.webServer># G" N: |8 m- C- U$ A- I( P
- ...
& T9 V7 [8 W4 U3 Z - <httpProtocol>
& j- ~9 |4 l B2 M: }$ Y - <customHeaders>
- Y" i1 s- m9 A4 D3 Y; G1 o - <add name="X-Frame-Options" value="SAMEORIGIN" />
6 v4 G ` C# @, p3 J# m3 a% R" j5 m+ r0 R - </customHeaders>
6 }/ G( e1 r0 `% B8 E - </httpProtocol>
4 K- M6 s( L* ~7 P; g+ r" G - ...) y* l$ X( U& g4 ]
- </system.webServer>
复制代码 6 c, @ z( r2 j& t6 g O
之所以说不人性化,是因为这样操作之后,别人iframe会显示服务器拒绝之类的信息,虽然是屏蔽了,但是自己网站也不能宣传了。
- n) h9 {3 L& P% w+ r" t) R& y7 Z) `1 j5 b: M2 U) M
3、推荐解决方法。/ v# M3 V9 B" E' C- \7 g( m
- <script language="javascript">
9 M, @: P5 H/ H2 I5 X - if(top != self){
$ U; V) {: E2 _& y - location.href = "http://xp6.org/iframe.html";
: b' M$ t+ k9 P - top.location.href=self.location.href;
g4 X/ C* x# K* T - }
0 W5 n1 @3 l) Q9 f0 ? X( e2 W - </script>
复制代码 ) X; l* [8 R8 G& [5 z
将上面的代码复制到需要屏蔽页面<head>里面加载。 h6 i% J$ ^+ o* y( ?- b
制作一个iframe.html页面里面放张网站宣传图片,加个链接,放到网站根目录。
. h9 M Q) v# l7 A; P, T+ ]5 a原理:
5 O. u' ^0 a4 F" h% `4 S, ]0 x第一步直接强制访问咱自己设定的iframe.html页面,如果网站禁用了JS跳转,那么执行到这一步就结束了,这样即使他们iframe了咱的网站,看到的只是咱的图片宣传页。
( j8 f: u2 m' M% l第二步,如果没有禁用JS,那么会接着执行top.location.href=self.location.href; ,然后跳转到我们的网址页面。8 Z8 Z9 f, Y) r- \/ C
目前测试这种方法没什么问题。
; `' f8 A) R0 C/ X d- U' Q. W& B& N$ e8 [1 ]& u: l. i
|
|