禁止网站被别人通过iframe引用

kmwl520 建站经验 0

镜像别人网站是非常讨厌的行为。其中有一种方法采用了 iframe 框架嵌套调用,那么如何阻止被iframe 调用呢?本文将分享一些方法。

 

方法一:js 方法。这种方法不可靠,不推荐使用

windows.location.href: 自己的地址;

self: 指代当前窗口对象,属于window最上层的对象;

location.href: 指的是某window对象的URL地址;

self.location.href: 指当前窗口的URL地址,去掉 self 默认为当前窗口的URL地址;

window.location.hreflocation.hrefself.location.href 是本页面地址;

本文禁止住转载。任何形式转载请联系作者(时光在路上 www.timezls.com)。时光在路上保留所有权利

 

parent.location.href: 上一层页面地址;

 

top.location.href: 最外(上)层的页面地址。

image.png

 

  1. <script type="text/javascript">

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

  3. </script>

  1. <script type="text/javascript">

  2.     if(top.location != self.location){

  3.     top.location = self.location;

  4.     }

  5. </script>

  1. <script type="text/javascript">

  2.     if(window.top.location != window.self.location){

  3.       alert('你想表达的内容');

  4.       window.top.location = window.self.location;

  5.     }

  6. </script>

  1. <script>

  2.     if (top.frames.length!=0){

  3.         top.location=self.document.location;

  4.     }

  5. </script>

 
  1. <script>

  2.     (function(window){

  3.         if (window.location !== window.top.location){

  4.         window.top.location = window.location;

  5.         }

  6.     })(this);

  7. </script>

  1. <script>

  2.     this.top.location !== this.location && (this.top.location = this.location);

  3. </script>

  1. <script type="text/javascript">

  2.      var url=window.location.href;

  3.      if(window!=parent){

  4.      parent.navigate(url);

  5.      }

  6.  </script>

 
  1. <script type="text/javascript">

  2.     if(top != self){

  3.       location.href = "about:blank";

  4.     }

  5. </script>

 
  1. <script type="text/javascript">

  2.    if(window!=parent){

  3.      parent.navigate(window.location.href);

  4.   }

  5. </script>

 

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

 

方法二:Meta 标签方法

本文禁止全文转载。任何形式转载请联系作者(时光在路上 www.timezls.com) Copyright © 2024. All Rights Reserved

X-Frame-Options 是一个HTTP 标头(header),用来告诉浏览器这个网页是否可以放在 iFrame内。使用 X-Frame-Options 可以一定程度上保障你的网页不会被放在恶意网站设定的 iFrame内,令用户成为点击劫持的受害人。例如:

  • X-Frame-Options: DENY (不要把任何网页放在iFrame 内);

  • X-Frame-Options: SAMEORIGIN(只有当架设 iFrame的 网站与发出 X-Frame-Options 的网站相同时才能显示发出 X-Frame-Options 网页的内容);

  • X-Frame-Options: ALLOW-FROM https://www.timezls.com/(只能放在 https://www.timezls.com/ 网站网页架设的 iFrame 内)。

 

  1. <meta http-equiv="X-Frame-Options" content="deny" />

 

并不是所有的浏览器都支持这个header 所以,低版本的浏览器仍然会被 iframe 成功:

  • IE 8+

  • Opera 10.50+

  • Safari 4+

  • Chrome 4.1.249.1042+ (Allow-From not yet supported)

  • Firefox 3.6.9 (or earlier with NoScript)

而且,可能会出现:X-Frame-Options may   your-url.html:1   only be set via an HTTP header sent along with a document. It may not be set inside <meta> 这样的错误信息。

 

本文禁止全文转载。任何形式转载请联系作者(时光在路上 www.timezls.com) Copyright © 2024. All Rights Reserved

后端程序处理方法

方法三:PHP 法

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

上面这种方法,使用 CDN 静态加速后会失效。

 

服务器端解决方法

方法四:Apache主机

  1. Header always append X-Frame-Options SAMEORIGIN

 

方法五:Nginx主机

  1. add_header X-Frame-Options "SAMEORIGIN";

 

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

  1. Header append X-FRAME-OPTIONS "SAMEORIGIN"

 

本文禁止全文转载。任何形式转载请联系作者(时光在路上 www.timezls.com) Copyright © 2024. All Rights Reserved

方法七:IIS方法
在web.config文件中加

  1. <system.webServer>

  2.   ...

  3.  <httpProtocol>

  4. <customHeaders>

  5. <add name="X-Frame-Options" value="SAMEORIGIN" />

  6. </customHeaders>

  7. </httpProtocol>

  8.     ...

  9. </system.webServer>


 国模图片  女生头像小清新 唯美优雅  台湾第一美女  美女图片素材  大奶牛美女 
 穿刺烧烤美女  人体艺术照片  中国模特  私拍 大尺度  美女动画片 
标签关键词:

免责声明

本站提供的一切软件、教程和内容信息仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。如果您喜欢该程序,请支持正版,购买注册,得到更好的正版服务。如有侵权请邮件xm520vip@gmail.com与我们联系处理。敬请谅解!

欢迎 发表评论:

请填写验证码