abo*_*sun 72 php python iframe frame http-headers
如何在页面加载期间找到我的页面作为框架嵌入到其他网站?我猜引用请求标题在这里无法帮助我?谢谢.
Mae*_*lyn 94
您无法从服务器端检查它,但您可以在页面加载后使用javascript检测它.比较top和self,如果他们不一样,你是在一个框架.
此外,一些现代浏览器尊重X-FRAME-OPTIONS标头,可以有两个值:
用户包括Google的Picasa,无法嵌入到框架中.
支持标题的浏览器,最小版本:
Fel*_*ing 40
Stackoverflow包含一些JS来测试它(master.js).这是它的相关部分:
if(top!=self){
top.location.replace(document.location);
alert("For security reasons, framing is not allowed; click OK to remove the frames.")
}
Run Code Online (Sandbox Code Playgroud)
但请记住,JS可以被禁用.
rvi*_*hne 26
对于现代浏览器,您可以使用CSP(内容安全策略),这是一种标准.以下标题将阻止文档在任何位置加载到框架中:
Content-Security-Policy: frame-ancestors 'none'
Run Code Online (Sandbox Code Playgroud)
(IE 11需要X-前缀).您还可以更改'none'为允许框架的原点,例如您自己的网站.
为了覆盖旧版浏览器,最好与@ Maerlyn的答案一起使用.
ima*_*man 14
您可以使用javascript阻止在iframe中加载您的网页
<script type="text/javascript">
if ( window.self !== window.top ) {
window.top.location.href=window.location.href;
}
</script>
Run Code Online (Sandbox Code Playgroud)
此代码将页面iframe的容器地址更改为您的页面地址,并强制容器显示您的页面.
或者,如果您不介意某些位置的内容但不想在特定网站上使用,则可以阻止特定域.例如,如果offendingdomain.com要嵌入您的内容,您可以这样做:
<script type="text/javascript">
if(document.referrer.indexOf("offendingdomain.com") != -1) {
window.location = "http://www.youtube.com/watch_popup?v=oHg5SJYRHA0";
}
</script>
Run Code Online (Sandbox Code Playgroud)
这将检查父文档的位置,看看是否offendingdomain.com嵌入了您的内容.然后,此脚本将该iframe发送到某个着名的YouTube视频作为惩罚.实际上他们只是Rick-Rolled自己.
| 归档时间: |
|
| 查看次数: |
60577 次 |
| 最近记录: |