终极解决禁止网站被别人通过iframe引用嵌套套入。防止别人镜像自己的网站

2018-08-18
0评论
/
6243阅读
爱搜啊

由于嵌入iframe的嵌入 使得网站变的更不安全, 如何能防止网页禁止被iframe嵌入呢?

首先看下知名网站使用的方法

<script type="text/javascript">
document.domain="qq.com";var _s_=new Date(),g_T={},siDomain="ctc.qzonestyle.gtimg.cn",g_iUin=499469859,g_iLoginUin=499469859;g_T.fwp=[_s_];document.namespaces&&document.namespaces.add&&(document.namespaces.add('qz', 'http://qzone.qq.com/'),document.namespaces.add('x', 'http://qzone.qq.com/'));var QZFL={};QZFL.event={};QZFL.event.getEvent=function(evt){var evt=window.event||evt,c,cnt;if(!evt&&window.Event){c=arguments.callee;cnt=0;while(c){if((evt=c.arguments[0])&&typeof(evt.srcElement)!="undefined"){break;}else if(cnt>9){break;}c=c.caller;++cnt;}}return evt;};QZFL.event.getTarget=function(evt){var e=QZFL.event.getEvent(evt);if(e){return e.srcElement||e.target;}else{return null;}};var QZFF_M_img_ribr=[];QZFL.media={reduceImgByRule:function(ew,eh,opts,cb){QZFF_M_img_ribr.push(QZFL.event.getTarget());},adjustImageSize:function(w,h,trueSrc,cb,errCallback){QZFF_M_img_ribr.push(QZFL.event.getTarget());},reduceImage:function(){QZFF_M_img_ribr.push(QZFL.event.getTarget());},getImageInfo:function(){QZFF_M_img_ribr.push(QZFL.event.getTarget());}};g_T.fwp[1] = new Date();</script>

这个是腾讯QQ空间使用的防嵌套方法

if(window.top !== window.self){ window.top.location = window.location;}

这个是淘宝使用的方法

这个要说下,其实今天要介绍,四种防iframe的方法

第一种要说的就是淘宝使用的办法

解决方案一:js方法

<script type="text/javascript">
if(self != top) { top.location = self.location; }
</script>
if (self == top) {
    var theBody = document.getElementsByTagName('body')[0];
    theBody.style.display = "block";
} else {
    top.location = self.location;
}

上面两种方法都是可以的

把上面的JS代码片段放到你页面的 head 中即可。

要特别说明下这种方法不是很靠谱,可以很轻松使这种方法失效。

只需要添加下面代码使JS代码失效,这种方法就没用了。

<script type="text/javascript" charset="utf-8">
document.write('<iframe seamless sandbox security="restricted" id="url_mainframe" frameborder="0" scrolling="yes" name="main" src="http://iqqoz.com" style="height:100%; visibility: inherit; width: 100%; z-index: 1;overflow: visible;"></iframe>');
</script>//把里面的http://iqqoz.com换成要嵌套的网址

解决方案二:Meta标签方法

在需要禁用iframe嵌套的网页head中添加下面代码

<meta http-equiv="X-FRAME-OPTIONS" content="DENY">

以上两种为前端处理方法,就我个人来说不推荐使用,不过这个也是因人而异的,没有绝对的好与差。

解决方案三:PHP方法

<?php header(‘X-Frame-Options:Deny'); ?>

在需要禁用iframe嵌套的PHP网页中添加上面代码

上面这种是后端程序处理方法。

解决方案四:Apache主机方法

配置 Apache 在所有页面上发送 X-Frame-Options 响应头,需要把下面这行添加到 'site' 的配置中:

Header always append X-Frame-Options SAMEORIGIN

解决方案五:Nginx主机方法

配置 nginx 发送 X-Frame-Options 响应头,把下面这行添加到 'http', 'server' 或者 'location' 的配置中:

add_header X-Frame-Options "SAMEORIGIN";

配置 IIS 发送 X-Frame-Options 响应头

添加下面的配置到 Web.config 文件中:

<system.webServer>
  ...
  <httpProtocol>
    <customHeaders>
      <add name="X-Frame-Options" value="SAMEORIGIN" />
    </customHeaders>
  </httpProtocol>
  ...
</system.webServer>

解决方案六:.htaccess方法

在网站根目录下的.htaccess文件中中加一句

if(window.top !== window.self){ window.top.location = window.location;}0

以上几种解决方案为服务器端解决方案。


本站附件分享,如果附件失效,可以去找找看

诚通网盘附件百度网盘附件


于2018-08-18发布