栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

PHP、Nginx、Apache中禁止网页被iframe引用的方法

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP、Nginx、Apache中禁止网页被iframe引用的方法

可以使用php或nginx等添加X-frame-Options header来控制frame权限
X-frame-Options有三个可选的值:

DENY:浏览器拒绝当前页面加载任何frame页面
SAMEORIGIN:frame页面的地址只能为同源域名下的页面
ALLOW-FROM:允许frame加载的页面地址

PHP代码:

header(‘X-frame-Options:Deny');

Nginx配置:

add_header X-frame-Options SAMEORIGIN

可以加在locaion中

location /
{
  add_header X-frame-Options SAMEORIGIN
}

Apache配置:

Header always append X-frame-Options SAMEORIGIN

使用后不充许frame的页面会显示一个白板。

IIS方法

在web.config文件中加


  ...
  
  
  
  
  
  ...

js方法

很多都是用这种方放,服务器端设置有时候有问题

if (self.frameElement && self.frameElement.tagName == "Iframe") {
  top.location.href=self.location.href;
}
if (window.frames.length != parent.frames.length) {
  top.location.href=self.location.href;
}
if (self != top) { 
  top.location.href=self.location.href;
}

meta标签方法

css禁止其他人的iframe,允许自己的


iframe{v:expression (this.src='about:blank',this.outerHTML='');}
#mine{v:expression() !important}

内容: 百度   126邮箱 网易

以上三个firame不允许

firame google 是我要的.


参考:

https://developer.mozilla.org/en-US/docs/Web/HTTP/X-frame-Options?redirectlocale=en-US&redirectslug=The_X-frame-OPTIONS_response_header

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/48089.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号