|
|
前面说了如何阻止 iframe里引用的网页自动跳转,已达到iframe页面的目的。& _1 K. M& \4 T- }1 l8 I
这次讲一下如何防止自己的网页被别人iframe。# H0 p+ y& M+ P2 n+ r% g
x# m' K) T6 l% ]# w1、这些方法都可行,但不是太可靠。
" Q& [: {. G0 g4 m5 x6 L- <script language="javascript">
; y& X( Q3 E" F9 M! G! `: S5 h - if( top.location != self.location) top.location.href=self.location.href;
8 \; l& ]1 N" K, p - </script>
复制代码 或
) q: {- y0 P5 Q q7 f- <script language="javascript">& S( G0 F& z) o
- if (top.location != location) top.location.href = location.href;
0 ?$ ~8 O9 n* c+ \ - </script>
复制代码 或
% M* u* l+ r. R! ] e- <script language="javascript"> a& u) V+ L+ Y9 {
- if (top.location != self.location) {top.location=self.location;}! A) O7 d( A$ d' [
- </script>
复制代码 或3 D: a( P0 m& R! ]7 `
- <script language="javascript">
6 o! [! S# d2 \. e - if (top.frames.length!=0) top.location=self.document.location;
+ L* a* b. K' @. k - </script>
复制代码 不可靠的原因:
8 C0 }! k0 D Z& }当别人用如下类似代码做IFRAME嵌套调用时,就可能躲过你的页面的javascript代码。% N/ w4 I! s4 ?7 S8 e0 N$ |6 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>
$ F' J) v8 c) F) p# C- t4 O4 q6 C, t - <script language="javascript"> # J' N0 f K0 ^& J$ q/ [! j8 n2 @* Q
- var location="";& t: a! w6 a' s. G T
- var navigate=""; w# y2 w2 F% ]1 ?
- frames[0].location.href="";3 \1 d& c; Q9 b4 C. G$ D$ C5 o
- </script>
复制代码 或3 Y- Y) D8 g; ?# P" A
- <iframe src="http://xp6.org/" class="t-iframe" scrolling="no" security="restricted" sandbox="">
复制代码 或( l1 h' s* T( q# b! r
( w* l' q: Z3 ]
- <script type="text/javascript" charset="utf-8">
?0 H) p7 z6 W$ g+ g* y" V0 ] - 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>');
+ m9 P. N7 p- t0 P" p0 ] - </script>
复制代码 2、代码层次的屏蔽,方法可行,但不是很人性化。/ x8 }$ o8 {- b( T
Meta标签方法
' A$ o3 A$ W+ \: W( a+ J' |- <meta http-equiv="X-FRAME-OPTIONS" content="DENY">
复制代码 PHP方法
' G+ \8 \. ^6 q; |1 r& ]4 h2 o- <?php header('X-Frame-Options:Deny'); ?>
复制代码 Apache主机方法
& y) [4 L f r! d# s ~- Header always append X-Frame-Options SAMEORIGIN
复制代码 Nginx主机方法5 m J, Y$ @1 c R$ F& z
- add_header X-Frame-Options "SAMEORIGIN";
复制代码
* d" m R( ^- a8 m8 a; X% x" ].htaccess方法
2 p1 E# G# J( x% j( P9 i在网站根目录下的.htaccess文件中中加一句: P7 D: k' b) i
- Header append X-FRAME-OPTIONS "SAMEORIGIN"
复制代码 5 X& M" p9 ?* G0 Z! `/ i- d
IIS方法5 n/ S( K+ ?4 e3 j
在web.config文件中加- <system.webServer>0 h; B( H( e$ i/ @1 h+ q: r
- ...+ T5 I9 d, Q2 a
- <httpProtocol>
; U9 u+ H9 E" q7 h$ D - <customHeaders>
; C( {4 x! P* s( K% z - <add name="X-Frame-Options" value="SAMEORIGIN" />
& `/ H+ _. K* f5 i9 A+ i - </customHeaders>
- W) ~4 |, V( i5 f# f - </httpProtocol>
. }7 `! V+ Z$ V4 S# F, |2 ^ - ...
% P3 K# h2 Z _/ z6 ?# T, O2 \ - </system.webServer>
复制代码 $ k5 z4 c* ^- @5 k" P" _. `% b
之所以说不人性化,是因为这样操作之后,别人iframe会显示服务器拒绝之类的信息,虽然是屏蔽了,但是自己网站也不能宣传了。$ \5 ] p0 H% J4 f" G
' A, J) P8 R/ J: Z$ l3、推荐解决方法。
; k' a0 C. f; H" d+ u* w3 J, ^4 `: Y/ d3 r- <script language="javascript"> * ]/ s( Y0 B; V& F6 [; ?
- if(top != self){
( Q) s( Y& r/ Q8 F+ D - location.href = "http://xp6.org/iframe.html"; 9 `. ?$ Y X1 Z
- top.location.href=self.location.href;
+ a* J/ [9 x' s1 J7 t6 H+ p8 @6 s. L - }
0 {6 L: o+ P" L5 D$ ^# s. ] - </script>
复制代码 $ ]( u4 h" X3 w' g+ W _
将上面的代码复制到需要屏蔽页面<head>里面加载。
$ g* J! O! x' }制作一个iframe.html页面里面放张网站宣传图片,加个链接,放到网站根目录。
4 S2 i. L, S$ [" v; J原理:' V( }0 Y1 S( Q( b( q/ M1 j0 A+ N& n
第一步直接强制访问咱自己设定的iframe.html页面,如果网站禁用了JS跳转,那么执行到这一步就结束了,这样即使他们iframe了咱的网站,看到的只是咱的图片宣传页。
/ [# t! @1 _! O4 `) j- I3 f2 t第二步,如果没有禁用JS,那么会接着执行top.location.href=self.location.href; ,然后跳转到我们的网址页面。
: t, I* g2 X ]9 O, G, {目前测试这种方法没什么问题。
, ]- t- e( \3 L6 g' n' O9 ]+ n- E9 e0 _' R3 M& o
|
|