|
|
前面说了如何阻止 iframe里引用的网页自动跳转,已达到iframe页面的目的。/ W: Q/ k; F/ l: |/ P/ `8 ?
这次讲一下如何防止自己的网页被别人iframe。
& H9 V8 J' c2 X- U, g. w# j4 f8 w/ n0 `
1、这些方法都可行,但不是太可靠。1 E& l) n$ S8 d3 H( w( L$ i
- <script language="javascript">
* Z3 k( p- n' f! L2 v. g - if( top.location != self.location) top.location.href=self.location.href;
* ?. W3 z. H+ g7 Z - </script>
复制代码 或
! w# m1 K( c( Q7 l5 H R- q) a- <script language="javascript">
0 t, Q% w! G R' S+ c - if (top.location != location) top.location.href = location.href;6 W$ _3 b& f; C( \7 i& g. z8 j
- </script>
复制代码 或3 u8 }2 V/ N7 `; |* F1 X# v
- <script language="javascript">7 E- [+ y* W! s7 j! }
- if (top.location != self.location) {top.location=self.location;}
, j2 {" |( s Z+ m+ w - </script>
复制代码 或* T+ A9 X' Y" M% b
- <script language="javascript">
6 ~7 G' s }: h% s& Y- \8 w - if (top.frames.length!=0) top.location=self.document.location;0 k3 m$ b5 W7 s+ d- h. E# ^
- </script>
复制代码 不可靠的原因:
7 r: Q: w, X1 ]' _当别人用如下类似代码做IFRAME嵌套调用时,就可能躲过你的页面的javascript代码。7 |. X3 F. D) 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>
) t6 ]' v1 U) y5 I) Y: M - <script language="javascript">
& N2 k! ^- e. G! v - var location="";/ X6 }& x# E2 q" J6 F1 `% m
- var navigate="";
: S# R" p/ x( C7 q: j- E5 ~ - frames[0].location.href="";( m2 N7 z- [& v8 E
- </script>
复制代码 或
/ D8 j" g7 S6 ~4 A2 r! A0 X- <iframe src="http://xp6.org/" class="t-iframe" scrolling="no" security="restricted" sandbox="">
复制代码 或
: Z. b% _! m P5 A2 u
# _" O# f) ?- t% ~ h1 V6 v- <script type="text/javascript" charset="utf-8">
, y) ]# K2 i$ U* ?! ]4 T$ 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>');3 G' k5 W3 t3 q0 \9 Y
- </script>
复制代码 2、代码层次的屏蔽,方法可行,但不是很人性化。
, e( b+ y! L/ V5 L2 [: \Meta标签方法
5 i' \. B2 B! p0 n" B- <meta http-equiv="X-FRAME-OPTIONS" content="DENY">
复制代码 PHP方法
( j! K' X% j$ L M2 Z- <?php header('X-Frame-Options:Deny'); ?>
复制代码 Apache主机方法5 c( a& e$ ?7 N5 B& Z; X# ~ o
- Header always append X-Frame-Options SAMEORIGIN
复制代码 Nginx主机方法7 m4 K3 s( s {5 A. Q6 E
- add_header X-Frame-Options "SAMEORIGIN";
复制代码
" d8 R6 U. f6 M v N.htaccess方法
8 h5 a L. |5 e1 {/ I4 ~) p- U在网站根目录下的.htaccess文件中中加一句
; l9 E" c( R6 G( ]- Header append X-FRAME-OPTIONS "SAMEORIGIN"
复制代码
/ g P3 l! G/ q& l3 `- O; VIIS方法7 f) d. x9 A2 E+ q+ n7 J0 w
在web.config文件中加- <system.webServer>
& X8 n) h4 c2 P+ q W2 X8 P - ...
; C; I+ N0 l+ ?: w/ c - <httpProtocol>
! H" I: z. s% D! K - <customHeaders>
5 P* J1 t( i: B( f5 z0 y' u- G - <add name="X-Frame-Options" value="SAMEORIGIN" /> : J+ Z8 X, N, [% ]! J
- </customHeaders> 5 f8 J& B `1 ~) u" H, ~
- </httpProtocol>
$ [& O8 f) @! v, y - ...- K9 x( J& l3 J9 h
- </system.webServer>
复制代码 4 v b$ s9 ]$ a! H0 A+ E
之所以说不人性化,是因为这样操作之后,别人iframe会显示服务器拒绝之类的信息,虽然是屏蔽了,但是自己网站也不能宣传了。% m! i& {* D P; ]+ K9 ~6 F
3 [2 x- [7 c! ?0 t2 D/ t& y" r
3、推荐解决方法。
. ]9 j1 b% o+ i) D# w- <script language="javascript">
) I5 `5 K# J& ` - if(top != self){9 E% @: S+ d) [& w
- location.href = "http://xp6.org/iframe.html";
3 _* y& }, h6 B - top.location.href=self.location.href;9 j, `* R7 |: D" O9 y% J& |6 _
- }
' E C5 H5 s' p3 f: i - </script>
复制代码 6 F. p+ ^0 h+ X
将上面的代码复制到需要屏蔽页面<head>里面加载。
/ Q' Z3 B6 W0 v- u# b制作一个iframe.html页面里面放张网站宣传图片,加个链接,放到网站根目录。
; G) M# d4 g3 }! a& @3 J# R l原理:
T% M+ i3 G+ d& P$ @, ?0 ^/ q第一步直接强制访问咱自己设定的iframe.html页面,如果网站禁用了JS跳转,那么执行到这一步就结束了,这样即使他们iframe了咱的网站,看到的只是咱的图片宣传页。% ]2 e3 N' ^% g2 @& q
第二步,如果没有禁用JS,那么会接着执行top.location.href=self.location.href; ,然后跳转到我们的网址页面。
0 |) [, O8 i3 I1 a/ Q3 b7 N+ a目前测试这种方法没什么问题。
" j4 O& G& z! I! O4 B/ L! m$ l# [& G- p+ I7 ?
|
|