防止别人 iframe 自己的页面

一、如果对方是静态调用 iframe,用 js 阻止即可,

<script>
if ( top !== self ) top.location.replace( self.location.href );
</script>


二、如果对方是动态调用的(类似于下方代码),又禁用了自己页面的 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://www.mypage.com/this.html" style="height:100%; visibility: inherit; width: 100%; z-index: 1;overflow: visible;"></iframe>');
</script>

可在 php 文件里加上 X-Frame-Options 响应头代码

header('X-Frame-Options:Deny');

X-Frame-Options 有三个值

分别是 “DENY”、“SAMEORIGIN”、“ALLOW-FROM http://domain.com/url.html”

DENY:表示该页面禁止 frame,即使是同域名的页面中嵌套也不允许。

SAMEORIGIN:表示该页面可以在同域名页面的 frame 中展示。

ALLOW-FROM url:表示该页面可以在指定来源的 frame 中展示。


三、踩坑!下面这种直接在 html 的 head 中加 meta 是没用的,切记。

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

打赏

取消

感谢支持 Savalone !

扫码支持

打开支付宝扫一扫,即可进行扫码打赏哦


「本文由 Savalone 原创或搜集整理发布,转载请遵守 CC BY-NC-ND 4.0 许可!」
  • 19