阻止其他网站的页面iframe引用自己的页面

很多时候,其他站长会偷懒直接通过iframe引用自己的页面,导致自己站点的不可预知的因素出现。在此,虾咪将教你通过js或者PHP远吗解决这个问题。
一、js脚本判断

在自己的页面写入如下代码
console.log(top==self)
如果自己的页面被嵌入iframe中或者frame中就打印false。
所以不想被引用可以如下写:
if(self!=top) top.location=self.location
示例:

<script type="text/javascript">
        if (self!=top){
             window.top.location.replace(self.location); //打开自己网站的页面
            }
</script>

注意:这段脚本,ie7-9 chroom firefox 都可以顺利执行
二、设置http请求头的X-Frame-Options
如果对方禁止了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.example.cOm/#.html" style="height:100%; visibility: inherit; width: 100%; z-index: 1;overflow: visible;"></iframe>');
</script>

其目的是动态的iframe加载,而且禁止了js代码运行,所以JS代码没有生效;那么,你将删掉刚才加入的js代码,在php源码中加入:
header(‘X-Frame-Options:Deny’);
X-Frame-Options可以设置三个值
1、DENY 代表页面不会能被嵌入到iframe或者frame里
2、SAMEORIGIN 页面只能被本站页面嵌入到iframe或者frame中
3、ALLOW-FROM uri 页面只能被制定的uri嵌入到iframe 或 frame中
至此,问题解决。

给TA打赏
共{{data.count}}人
人已打赏
PHP技术

ProRank 您的网站全能分析专家

2018-7-7 12:20:11

PHP技术

区分PHP动态模式和静态模式

2018-7-14 20:22:30

    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索