|
|
前面说了如何阻止 iframe里引用的网页自动跳转,已达到iframe页面的目的。; G* \3 e- F5 v# i. {3 x, _
这次讲一下如何防止自己的网页被别人iframe。
% G( T$ o2 T) }( E% Z* e
" S# g; L1 E- G; M" u5 ^1、这些方法都可行,但不是太可靠。1 g$ F% d4 t: \/ r$ i# ^) `# _- e
- <script language="javascript">0 t+ }# E6 i' s% [, b$ h
- if( top.location != self.location) top.location.href=self.location.href;; c* c8 o5 j" I$ h
- </script>
复制代码 或
; q5 `3 r2 X- s: S! s- <script language="javascript">! e* p$ I0 L( t/ H
- if (top.location != location) top.location.href = location.href;8 v6 Y" w( s1 t5 [ x
- </script>
复制代码 或2 z$ e& t3 S2 j7 O2 N( r e
- <script language="javascript">
4 r) Z ^9 S% s" l - if (top.location != self.location) {top.location=self.location;}
, e" g+ p. `7 z; t+ {6 L - </script>
复制代码 或
! g8 C+ E+ X2 q" k7 I- <script language="javascript">
8 @3 L8 }- Q" @" n/ x5 l& | - if (top.frames.length!=0) top.location=self.document.location;
* ]: c7 O8 e. P& T/ I - </script>
复制代码 不可靠的原因:
/ f* P2 g; {) S- S当别人用如下类似代码做IFRAME嵌套调用时,就可能躲过你的页面的javascript代码。8 ~# U% |/ u, L2 ]: ]) 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>
- l' z3 D* m; E3 H5 z# b* d, @$ [ - <script language="javascript">
6 o( J# J/ O9 T$ N8 @3 S2 [ - var location="";% ]7 S5 C) s4 }5 w' {2 x
- var navigate="";+ x! i" r) U8 y, c- ~# z+ E
- frames[0].location.href="";- t s5 Z4 f3 S7 ^- m4 S6 K3 e
- </script>
复制代码 或& c/ }1 J% w$ n7 g# M( ^3 x0 p
- <iframe src="http://xp6.org/" class="t-iframe" scrolling="no" security="restricted" sandbox="">
复制代码 或# d( [5 P% V" A
! @) m- w& W% I, y
- <script type="text/javascript" charset="utf-8">+ h. m; W3 E- X6 J i& P" N
- 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 _! a; h- p/ n- I- F2 a - </script>
复制代码 2、代码层次的屏蔽,方法可行,但不是很人性化。) }) c) z. n3 s0 }
Meta标签方法
& W O0 A: A7 q8 d2 V# U. E- <meta http-equiv="X-FRAME-OPTIONS" content="DENY">
复制代码 PHP方法
! q! q6 n% r+ S2 s" a O/ D6 s# {- <?php header('X-Frame-Options:Deny'); ?>
复制代码 Apache主机方法 L$ ]4 }7 u0 r% t
- Header always append X-Frame-Options SAMEORIGIN
复制代码 Nginx主机方法
+ n$ d( N4 L1 n# y! q- add_header X-Frame-Options "SAMEORIGIN";
复制代码
2 V' m3 V8 y/ ]% K.htaccess方法7 g @3 F* g+ ]; s
在网站根目录下的.htaccess文件中中加一句
! t0 A! R# R: B" T' v2 T- Header append X-FRAME-OPTIONS "SAMEORIGIN"
复制代码
. e9 o% |( z/ f+ n: n/ n: {' LIIS方法
6 p- _6 e, h$ A在web.config文件中加- <system.webServer>
i5 b9 g2 \. }' W# E. k6 c - ...
' k4 d$ d7 c! e - <httpProtocol>
# h! g! N0 V9 y% L# v - <customHeaders>
5 R7 o7 \ \7 |' C6 q- S' I - <add name="X-Frame-Options" value="SAMEORIGIN" /> 3 R$ N* P9 M% n4 c1 }: E
- </customHeaders>
0 W/ l! O" `$ K/ G+ ^ - </httpProtocol>
0 w M% _& T1 n% h9 ]% S+ l3 m, G - ...; z" _3 E+ |1 G1 g6 i( H, _
- </system.webServer>
复制代码
2 R* A1 O; V* A+ Y$ {之所以说不人性化,是因为这样操作之后,别人iframe会显示服务器拒绝之类的信息,虽然是屏蔽了,但是自己网站也不能宣传了。* ]' w( `+ [; M+ w1 B, J1 ?1 z' [
C6 Q3 ?5 |& X+ Y$ i3、推荐解决方法。
/ j1 R. c: r- a5 f+ Y6 U- <script language="javascript">
" h& o$ x' F( g: u4 k/ F - if(top != self){
: w7 r! ~$ K% p# q+ z - location.href = "http://xp6.org/iframe.html"; 1 C) {2 F0 j# P3 F
- top.location.href=self.location.href;
* d8 r$ ?) S* i4 V, `3 o - }
# `( o' k2 b' I& G. Q; c - </script>
复制代码 / X* z9 C8 L- W4 }
将上面的代码复制到需要屏蔽页面<head>里面加载。
. @ r1 N$ ?1 b5 [$ }; w制作一个iframe.html页面里面放张网站宣传图片,加个链接,放到网站根目录。0 ~! }/ F* M8 }( a1 g3 f* B
原理:/ @. |" t$ p1 B5 [ w8 d" X
第一步直接强制访问咱自己设定的iframe.html页面,如果网站禁用了JS跳转,那么执行到这一步就结束了,这样即使他们iframe了咱的网站,看到的只是咱的图片宣传页。
6 I9 Z i5 k6 \ M& |3 `2 s3 T第二步,如果没有禁用JS,那么会接着执行top.location.href=self.location.href; ,然后跳转到我们的网址页面。
6 [( c* t( A% }目前测试这种方法没什么问题。4 g/ Q$ H& K6 t. W$ G
1 [* C' n7 B8 k1 @, y6 @3 i
|
|