如何知道我的页面是否在facebook iframe中运行 [英] How to know if my page is running in a facebook iframe or not

查看:83
本文介绍了如何知道我的页面是否在facebook iframe中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个运行独立的网站,并在iframe上作为一个Facebook应用程序
我想知道什么是最佳实践,以检查我的页面是否在页面加载之前在一个facebook iframe中运行所以我可以预设相关的CSS和其他变量



谢谢。

解决方案

有几种方法可以解决这个问题。如果您不担心安全性(即,您真的只想知道如何格式化页面,而不是决定要显示的内容),那么您最好的选择是使用不同的网址进行Facebook访问。例如,如果您的独立站点是www.mysite.com,您可以将fb.mysite.com或www.mysite.com/fb配置为指向相同的位置,然后在应用程序设置中使用备用版本。您的服务器代码可以轻松查看正在访问的网址版本,并据此采取行动。当然,你必须注意你的链接,以确保它们保持正确的前缀。



另一种方法是使用signed_request进行讨论,设置一个cookie(或会话)当它存在以指示Facebook访问。每个页面顶部都会包含一些JavaScript代码,以确保该页面位于一个iframe中。如果没有,则代码立即重定向到当前页面,其中添加了一个参数,如?clearfb = 1,这将使服务器清除cookie /会话并以外部格式输出页面。


I'm currently developing a site which runs standalone and as a facebook app on an iframe I was wondering what whold be "best practice" for checking if my page is ran in a facebook iframe before the page loads so I can preset the relevant CSS and other variables

Thanks.

解决方案

There are a couple of ways to approach this. If you're not concerned about security (i.e. you really only want to know how to format the page rather than deciding what content to show) then your best bet may be to use a distinct url for Facebook access. For example if your standalone site is www.mysite.com you can configure fb.mysite.com or www.mysite.com/fb to point to the same place, then use the alternate version in your app settings. Your server code can then easily check which url version is being accessed and act accordingly. Of course you have to take some care with your links to make sure they maintain the correct prefix.

Another way is to use signed_request as discussed, setting a cookie (or session) when it is present to indicate a Facebook access. The trick there is to also include a bit of javascript code at the top of each page that checks to make sure the page is within an iframe. If not, then the code immediately redirects back to the current page with a parameter added like "?clearfb=1" which will tell the server to clear the cookie/session and output the page in external format.

这篇关于如何知道我的页面是否在facebook iframe中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆