使用PHP访问Facebook会话 [英] Access Facebook Session using PHP

查看:94
本文介绍了使用PHP访问Facebook会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正努力在我的网站上使用PHP保持Facebook会话活跃。

I am struggling to keep the Facebook session alive using PHP on my website.

我使用JavaScript SDK和PHP SDK构成我的应用程序的基础。

I use both the JavaScript SDK and the PHP SDK to form the basis of my app.

我遇到的问题是,当Facebook会话结束时,我的PHP脚本认为您已退出Facebook。但是,只要我使用JavaScript SDK调用 FB.init(),会话就会恢复生机。

The problem I am having is that when the "Facebook session" ends, my PHP script believes that you are logged out of Facebook. But, as soon as I call the FB.init() using the JavaScript SDK, the session comes back to life.

无论如何使用PHP SDK实现相同的目标吗?或者我可以在Facebook会话上设置自定义到期时间吗?

Is there anyway to achieve the same using the PHP SDK? Or can I set a custom expiry time on the Facebook session?

似乎会话过期时间设置为2小时但我不确定。我不认为调用PHP api会有所作为。我需要更清楚地解释发生了什么。基本上,如果您到达我的主页,您可以选择通过Facebook登录。如果你这样做,一切正常!一旦您登录并且您已经授权该应用程序,这可以在会话到期之前完成。当会话到期时,似乎PHP SDK无法确定您是否通过Facebook登录,但是,Javascript SDK是。我使用getUser()作为PHP SDK。


换句话说,因为会话已经过期,PHP认为您不再通过Facebook登录。无论会话是否存在,Javascript SDK都能够检测您是否已登录。当它意识到你的时候,它会以任何方式重新创建会话。但是为了通过PHP获取会话,显然需要刷新页面。这是我遇到的问题,因为页面显示基于您的Facebook登录的内容,我需要PHP SDK才能重新创建会话,因此无需刷新页面。

It seems that the session expire time is set to 2 hours but I am not certain about this. I don't think calling the PHP api will make a difference. I need to explain a little clearer what is happening. Basically, if you arrive at my home page, you get the option to login via Facebook. If you do, this all works fine! Once you are logged in and you have authorized the app, this is okay until the session expires. When the session expires, it seems that the PHP SDK is unable to determine whether or not you are logged in via Facebook, however, the Javascript SDK is. I use getUser() for the PHP SDK.

In other words, because the session has expired, PHP thinks that you are no longer logged in via Facebook. The Javascript SDK is able to detect whether or not you are logged in, regardless of whether the session is there. When it realises you are, it recreates the session any way. But in order for the session to be picked up by PHP, the page obviously needs to be refreshed. This is the problem I have, because the page displays content based on your Facebook login, I need the PHP SDK to be able to recreate the session as well, so that it is not necessary to refresh the page.

推荐答案

好的...不确定你的会话为什么会这样做的确切细节 - 我必须说明我不是很精通在PHP会话中并且没有丰富的经验。


我的建议是让JavaScript SDK完成它的工作......让它成功地重新检测你的会话完成后,对您的服务器进行AJAX调用。在处理该调用时,您可以创建并重新启动PHP SDK,从而恢复会话。


此外,您可以调用 FB.getAuthResponse 定期确保用户会话仍然有效(至少在JavaScript SDK中)。



来自 Fb.getLoginStatus()文档

Ok... Not to sure what the exact details of why your session are acting like this - I must state too that I am not very well versed in PHP sessions and have not has extensive experience with them.

My suggestion would be to let the JavaScript SDK do its work... let it "re-detect" your session successfully and after it has done so, make an AJAX call to your server. In the processing of that call you can create and re-initiate the PHP SDK hence reviving your session.

Additionally you could call the FB.getAuthResponse periodically to ensure that the users session is still valid ( at least in the JavaScript SDK ).

From the Fb.getLoginStatus() documentation :

{
    status: 'connected',
    authResponse: {
        accessToken: '...',
        expiresIn:'...',
        signedRequest:'...',
        userID:'...'
} }




通过测试在
响应对象中存在authResponse对象,您可以确定您的应用已知用户,并且您可以开始进一步调用Facebook API。 如果
authResponse对象不存在,则该用户未登录
Facebook,或者未授权您的应用。

这篇关于使用PHP访问Facebook会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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