|
前面说了如何阻止 iframe里引用的网页自动跳转,已达到iframe页面的目的。1 c; i0 Z8 S) G$ p9 x
这次讲一下如何防止自己的网页被别人iframe。& J! y4 r& Z8 z+ x+ t2 c
! G& i+ L( m5 y; p. p0 F1、这些方法都可行,但不是太可靠。) ^1 l. H5 F: h
- <script language="javascript">& Y) P+ a+ q& j1 @( h
- if( top.location != self.location) top.location.href=self.location.href;
. j5 V4 d, P, @) z" E1 Z: i - </script>
复制代码 或, J5 ]) Q! c; E2 b
- <script language="javascript">$ }8 R+ ?' ^* ~
- if (top.location != location) top.location.href = location.href;
7 E G# U* {' M+ ?: E - </script>
复制代码 或
5 C8 {* Z2 `4 n, {) G, S- <script language="javascript">
& @8 v. u$ ~! R. ] - if (top.location != self.location) {top.location=self.location;}
$ ]* ~/ W5 u! S+ \( x/ I9 `3 z - </script>
复制代码 或. S' S5 {8 K: ^0 j6 |
- <script language="javascript">! C% x, `9 t! G( U& [* w m
- if (top.frames.length!=0) top.location=self.document.location;7 B6 W0 j2 J% T2 h, c- Q! e* c
- </script>
复制代码 不可靠的原因:
5 T" v9 F5 b3 }* @( K% B$ ]当别人用如下类似代码做IFRAME嵌套调用时,就可能躲过你的页面的javascript代码。
. F( ~, U7 [/ Q6 q- <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>
8 \* J) a# ^& ?2 W - <script language="javascript">
& E0 ?# A5 {( k* L - var location="";6 |$ y" t9 s: S5 Y0 W- \
- var navigate="";, ~* `/ L- l9 \: b& i
- frames[0].location.href="";
2 T9 C4 B m' A - </script>
复制代码 或2 s! D% }5 k/ t1 L. K/ p( q
- <iframe src="http://xp6.org/" class="t-iframe" scrolling="no" security="restricted" sandbox="">
复制代码 或4 C7 ~/ e, a% } S
3 E! q7 M$ K/ f! w) y1 _
- <script type="text/javascript" charset="utf-8">7 e+ c" Z& f5 u$ V, L* W( @8 }- d1 ]1 j
- 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>');! H6 M7 E9 h6 y4 j& y3 H" ]8 ?( o
- </script>
复制代码 2、代码层次的屏蔽,方法可行,但不是很人性化。
4 L9 p% `; l! q9 k9 {$ a* V: wMeta标签方法. d4 s/ C& q6 M3 ^& `" S% ~
- <meta http-equiv="X-FRAME-OPTIONS" content="DENY">
复制代码 PHP方法; X6 ~3 k8 F2 J; `
- <?php header('X-Frame-Options:Deny'); ?>
复制代码 Apache主机方法1 K1 _! [* q) {5 S% h! V
- Header always append X-Frame-Options SAMEORIGIN
复制代码 Nginx主机方法& x1 j, h2 i( R: a+ F4 j
- add_header X-Frame-Options "SAMEORIGIN";
复制代码
, \9 k) ]0 L' P! n! A5 K. R.htaccess方法
0 L3 Y( ]# {6 U4 m1 n( s/ S' {6 U在网站根目录下的.htaccess文件中中加一句- u" f6 g$ b" a$ a
- Header append X-FRAME-OPTIONS "SAMEORIGIN"
复制代码
" i% R5 h, M8 X$ |. ^/ @9 JIIS方法
/ E3 P+ U: O) d6 n3 M在web.config文件中加- <system.webServer>$ D i& |! N; W( v
- ...
: W9 t# G6 K2 p+ W& i - <httpProtocol>
) A5 l+ x* y- m+ K7 [7 [0 V - <customHeaders># {. O" n4 {6 j
- <add name="X-Frame-Options" value="SAMEORIGIN" /> ) o# m1 w1 ?8 B+ U& g% L
- </customHeaders>
, |) r' M2 r6 c' C - </httpProtocol>
0 ?* f R3 ^- L8 z8 p2 G% Z" `% P - ...( H" C b$ M: B2 \: A8 V
- </system.webServer>
复制代码
% i3 D- E) C3 j1 ?/ K; x6 s之所以说不人性化,是因为这样操作之后,别人iframe会显示服务器拒绝之类的信息,虽然是屏蔽了,但是自己网站也不能宣传了。" ~ F" J, W& t7 _
5 e9 ]) g' Z) T# Q, {9 G
3、推荐解决方法。
. a/ r" C: W. q- <script language="javascript"> % ]9 a1 K: E5 O, O- g7 S
- if(top != self){
; c# P2 H; }; _/ E - location.href = "http://xp6.org/iframe.html";
Z/ y$ |/ D8 E# }$ x - top.location.href=self.location.href;0 O+ U _4 |5 O! ]% \" q% ~, N% w
- }
% ^& I( w) q) l, m; n - </script>
复制代码
4 d9 c" l0 e9 M6 h5 y6 r将上面的代码复制到需要屏蔽页面<head>里面加载。! E1 Y+ }# z) r$ A6 u- K, d7 d
制作一个iframe.html页面里面放张网站宣传图片,加个链接,放到网站根目录。7 G& Q) H( G/ j# g* r
原理:
0 R1 o$ h; I, \1 F# V7 O' }第一步直接强制访问咱自己设定的iframe.html页面,如果网站禁用了JS跳转,那么执行到这一步就结束了,这样即使他们iframe了咱的网站,看到的只是咱的图片宣传页。) m. H+ Y5 b2 C' C- F. W
第二步,如果没有禁用JS,那么会接着执行top.location.href=self.location.href; ,然后跳转到我们的网址页面。
- _* I5 c Y1 e% e4 y1 y6 P3 w目前测试这种方法没什么问题。. H4 w) g* L6 Q0 h" E& v
. ]! p+ [; c+ E- q4 f |
|