|
|
前面说了如何阻止 iframe里引用的网页自动跳转,已达到iframe页面的目的。8 j1 }: b% u0 w1 R
这次讲一下如何防止自己的网页被别人iframe。. `4 P2 w# n: N
/ W. Z7 r& A- M: u
1、这些方法都可行,但不是太可靠。
/ \2 g& H- O: g" W& F- <script language="javascript">
. k+ e( p# q1 b. t1 b - if( top.location != self.location) top.location.href=self.location.href;
8 [6 p" ^ h) M% J' n$ R - </script>
复制代码 或2 c! F+ J9 g" {. u: E% n" V# ^
- <script language="javascript">0 N s) ^9 a% Z% z+ ^# D
- if (top.location != location) top.location.href = location.href;
0 U! {! {' W% c7 p z: S - </script>
复制代码 或3 X% c3 p5 q9 e. s
- <script language="javascript">& B4 ]: ~+ o" f+ i: l7 ?& I. a u
- if (top.location != self.location) {top.location=self.location;}
. I6 C5 E1 W' a7 V) L - </script>
复制代码 或
' t" L4 u8 K' ]/ s& a- <script language="javascript">
. y& f' Q5 q2 `% F9 M - if (top.frames.length!=0) top.location=self.document.location;
8 _ h, K& O! V+ F! I - </script>
复制代码 不可靠的原因:* i3 Z2 ~% u3 I! h( p
当别人用如下类似代码做IFRAME嵌套调用时,就可能躲过你的页面的javascript代码。* V& C& \/ y; v: L5 ~4 K
- <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>
) H( O: S# W, |8 y - <script language="javascript">
6 X! [* H1 X$ n% I% y! e; |5 ` - var location="";
, D; K' ?5 d/ [ W1 p, ]) Y0 e) K - var navigate="";
& ?, O, N* U# s - frames[0].location.href="";
! ]+ ^- |" P q$ i - </script>
复制代码 或6 K% f: N& R( z! l5 L+ K
- <iframe src="http://xp6.org/" class="t-iframe" scrolling="no" security="restricted" sandbox="">
复制代码 或
1 u0 p$ x: _ U7 ?" s* @: Q( Y$ r- p* V( i
- <script type="text/javascript" charset="utf-8">2 D1 y) z% T" q0 X* g; V* ^$ f
- 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>');! B( V0 `# a( O/ P! ^% U. E8 I- j
- </script>
复制代码 2、代码层次的屏蔽,方法可行,但不是很人性化。# ` C7 @( v, }1 o' f( z3 Z4 R
Meta标签方法
. h" V( Q; D* x- v, w: M% r- <meta http-equiv="X-FRAME-OPTIONS" content="DENY">
复制代码 PHP方法
" v8 ]( v( H; P$ Q1 Q1 Y- <?php header('X-Frame-Options:Deny'); ?>
复制代码 Apache主机方法
6 s0 o- }$ p& r/ }- Header always append X-Frame-Options SAMEORIGIN
复制代码 Nginx主机方法$ i' l; Y& q* P
- add_header X-Frame-Options "SAMEORIGIN";
复制代码
d- c$ L1 ~1 r# {3 R5 l.htaccess方法
9 a* z' A: s% a4 N0 {+ z* C. b/ f4 @在网站根目录下的.htaccess文件中中加一句% J( s( x6 H1 a3 C6 P
- Header append X-FRAME-OPTIONS "SAMEORIGIN"
复制代码 9 j7 M0 d2 j. W4 n: Y5 _5 z8 y
IIS方法& T) q1 k8 k9 F q# d
在web.config文件中加- <system.webServer>
( _! r( x K$ I( F( z& t6 N - ...
$ V, a4 f: \# O! j/ g+ T - <httpProtocol>
% R2 S3 u% s/ T$ c - <customHeaders>
# O W0 K2 `: U; r% g8 z - <add name="X-Frame-Options" value="SAMEORIGIN" /> " v4 |' i: y" b
- </customHeaders> . B- t0 V) {% p, i2 Y! q
- </httpProtocol> 0 n% @5 ~2 s! D' P
- ...
3 _* b. d1 o+ w% ?6 m8 m - </system.webServer>
复制代码 , B. r: Z( h$ Z
之所以说不人性化,是因为这样操作之后,别人iframe会显示服务器拒绝之类的信息,虽然是屏蔽了,但是自己网站也不能宣传了。( I+ z0 y5 m( j; N9 g
! O6 _* J9 |" M3 s5 i" n2 \/ @
3、推荐解决方法。" O. \& c* T# |! C( s( _
- <script language="javascript"> . @; P! i- Y# ~( M
- if(top != self){- T5 \* ^5 Z- S; {
- location.href = "http://xp6.org/iframe.html";
# L& r. x+ X1 g$ t# |. |+ ^' ~' O - top.location.href=self.location.href;
. w. b) B4 G# z- M - } 8 O8 \& `! B4 s# Y' A }
- </script>
复制代码
% b; p# x) s4 U" b& u, p# e将上面的代码复制到需要屏蔽页面<head>里面加载。
1 s. Z, C2 d) x; N1 P1 ~+ i制作一个iframe.html页面里面放张网站宣传图片,加个链接,放到网站根目录。7 r8 h4 h* j q- {
原理:
1 y" ?/ I4 c& Q2 s! Z: `第一步直接强制访问咱自己设定的iframe.html页面,如果网站禁用了JS跳转,那么执行到这一步就结束了,这样即使他们iframe了咱的网站,看到的只是咱的图片宣传页。% S. b1 x; S0 M8 ]) D
第二步,如果没有禁用JS,那么会接着执行top.location.href=self.location.href; ,然后跳转到我们的网址页面。1 f4 o. b. N& N( a4 _
目前测试这种方法没什么问题。% Q W( K# c+ {' n1 }
2 I! B0 ^* K# V- B0 R6 ?: V
|
|