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

发布时间:2021-06-30 17:24:13 作者:chen
来源:亿速云 阅读:166

这篇文章主要讲解了“PHP、Nginx、Apache中禁止网页被iframe引用的方法是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“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文件中加

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

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标签方法

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

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

<style type="text/css">
iframe{v:expression (this.src='about:blank',this.outerHTML='');}
#mine{v:expression() !important}
</style>
<body> 内容:<iframe src="http://www.baidu.com"></iframe> 百度 <iframe src="http://www.126.com/"></iframe>  126邮箱<iframe src="http://www.163.com"></iframe> 网易<p>以上三个firame不允许</p> <p>firame google 是我要的.</p> <p><iframe id="mine" name="myfirame" src="http://www.google.com/" width=800 height=400></iframe></p>

参考:

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

感谢各位的阅读,以上就是“PHP、Nginx、Apache中禁止网页被iframe引用的方法是什么”的内容了,经过本文的学习后,相信大家对PHP、Nginx、Apache中禁止网页被iframe引用的方法是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. linux中Apache如何禁止解析php
  2. LAMP中Apache如何禁止解析php

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php nginx apache

上一篇:php如何生成QRcode

下一篇:Java中怎么实现BIO阻塞式网络编程

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》