# 如何防止别人iframe自己的网站

*Published:* 2023-11-15
*Author:* 客服001

#### 温馨提醒

如果文章内容或图片资源失效，请留言反馈，我们会及时处理，谢谢

本文最后更新于2023年11月15日，已超过 180天没有更新



一、如果对方是静态调用 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">
```