简体   繁体   English

我怎么知道我的应用程序正在Facebook内运行

[英]How do I know my app is being Run inside Facebook

If I have an app and it is run inside facebook. 如果我有一个应用程序,它在Facebook内运行。 the first load I will get the 第一次加载我会得到的

$_REQUEST['signed_request'] 

but if you click on any link inside my app and navigate within the app you will lose that $_REQUEST['signed_request'] . 但如果你点击我的应用程序内的任何链接并在应用程序中导航,你将失去$_REQUEST['signed_request']

Is there any other way to know if my app is being run in a browser or inside facebook? 有没有其他方法可以知道我的应用程序是在浏览器中运行还是在Facebook内运行?

You can continue to pass the signed_request around. 您可以继续传递signed_request Within your app, all your links should end with ?signed_request=<whatever> (or &signed_request=<whatever> if there is already a query string), and all your POST forms should include signed_request as a hidden input. 在您的应用程序中,您的所有链接应以?signed_request=<whatever> (或者如果已存在查询字符串,则为&signed_request=<whatever> ),并且所有POST表单都应包含signed_request作为隐藏输入。 Then you will always have access to signed_request . 然后,您将始终可以访问signed_request

You can save value of signed_request in php session , something like this: 您可以在php会话中保存signed_request的值,如下所示:

session_start();
if (isset($_REQUEST['signed_request'])){
  $_SESSION['signed_request'] = $_REQUEST['signed_request'];
}

Later you can check if signed_request value is saved in session: 稍后您可以检查session_request值是否保存在会话中:

if (isset($_SESSION['signed_request'])){
  //do something
}

If you really only need to know whether your app is being accessed through Facebook, the simplest way is to use a unique url in your app settings -- either a unique hostname like fb.yoursite.com, or a unique directory name like www.yoursite.com/fbapp/ . 如果您真的只需要知道您的应用程序是否通过Facebook访问,最简单的方法是在您的应用程序设置中使用唯一的URL - 或者像fb.yoursite.com这样的唯一主机名,或者像www这样的唯一目录名。 yoursite.com/fbapp/。 Then you configure your server so the unique hostname/directory points the same place as the regular hostname/directory. 然后配置服务器,使唯一的主机名/目录指向与常规主机名/目录相同的位置。 That way the same files can be reached either way, but your scripts can check the $_SERVER info to tell whether it's a Facebook request. 这样可以通过任何一种方式访问​​相同的文件,但是您的脚本可以检查$ _SERVER信息以判断它是否是Facebook请求。

HOWEVER...unless you are strictly dealing with non-authorized access, you have to consider whether that will really solve anything. 但是......除非你严格处理非授权访问,否则你必须考虑这是否能解决任何问题。 If you have to "detect" things like this, you must not be carrying any persistent state info, and your user will lose his "identity" as soon as he goes to page 2. So most likely, what you need to be considering is not a way to tell on each page whether the user is in Facebook, but rather a way to parse all the info you need on the first page and then make that available in all other pages. 如果你必须“检测”这样的事情,你不能携带任何持久的状态信息,并且你的用户一旦进入第2页就会失去他的“身份”。所以很可能,你需要考虑的是不是在每个页面上告诉用户是否在Facebook中的方式,而是在第一页上解析您需要的所有信息然后在所有其他页面中使用的方法。 Many people use PHP sessions for this. 许多人使用PHP会话。 Personally I think that's a bad idea, and would do something more like what Ben suggested in his answer (using GET and POST to pass the info you need). 就个人而言,我认为这是一个坏主意,并且会做更像Ben在他的回答中提出的建议(使用GET和POST来传递你需要的信息)。

Personally, a very simple yet effective solution I've used is some client side javascript (which can obviously be disabled etc) - this works nicely for a simple redirect and it won't work in every case. 就个人而言,我使用的一个非常简单但有效的解决方案是一些客户端javascript(显然可以禁用等) - 这很适用于简单的重定向,并且它不适用于所有情况。

// Redirect if the page is not an iframe
if (window.location == window.parent.location){
    window.top.location = '{{ facebook_tab_url }}';
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您知道我如何使用PHP对Facebook应用进行身份验证吗? - Do you know how I can authenticate an app for Facebook in PHP? 我如何在我的heroku静态应用程序中运行php文件 - How do i run php file in my heroku static app 如何知道我要通过访问令牌访问的Facebook应用 - How to know Facebook app I'd through access token 我怎么知道我是否被黑客入侵了? - How do I know if I'm being hacked? 我怎么知道我的页面中的facebook按钮是否被选中 - How can I know if facebook like button in my page is checked PHP SDK-如何知道Facebook上的人是否在Facebook上安装并授权了我的应用程序? - PHP SDK-How to know whether or not person from Facebook has installed and authorized my app on Facebook? 我如何知道准备语句是否正在缓存? - How do I know if a Prepared Statement is being Cached? 在 Laravel 中,我如何知道客户当前的费用是多少? - In Laravel, how do I know what a customer is currently being billed? 我如何知道会话是否已由Redis成功管理? - How do I know if sessions are successfully being managed by redis? 当我的Symfony2应用使用缓存时,什么时候不使用缓存? 我怎么知道? - When my Symfony2 app is using cache, when not? How do I know?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM