使用 PHP 访问 Facebook 会话
我正在努力在我的网站上使用 PHP 来保持 Facebook 会话的活动。
我使用 JavaScript SDK 和 PHP SDK 来构成我的应用程序的基础。
我遇到的问题是,当“Facebook 会话”结束时,我的 PHP 脚本认为您已从 Facebook 注销。但是,一旦我使用 JavaScript SDK 调用 FB.init()
,会话就会恢复正常。
有没有办法使用 PHP SDK 来实现相同的目的?或者我可以在 Facebook 会话上设置自定义到期时间吗?
摘自评论
会话过期时间似乎设置为 2 小时,但我对此不确定。我不认为调用 PHP api 会有什么不同。我需要更清楚地解释发生了什么。基本上,如果您到达我的主页,您可以选择通过 Facebook 登录。如果你这样做,这一切都很好!登录并授权该应用程序后,在会话过期之前就可以了。当会话过期时,PHP SDK 似乎无法确定您是否通过 Facebook 登录,但 Javascript SDK 可以。我将 getUser() 用于 PHP SDK。
换句话说,由于会话已过期,PHP 认为您不再通过 Facebook 登录。 Javascript SDK 能够检测您是否登录,无论会话是否存在。当它意识到您存在时,它会以任何方式重新创建会话。但为了让 PHP 获取会话,显然需要刷新页面。这是我遇到的问题,因为页面根据您的 Facebook 登录显示内容,我需要 PHP SDK 也能够重新创建会话,这样就不需要刷新页面。
I am struggling to keep the Facebook session alive using PHP on my website.
I use both the JavaScript SDK and the PHP SDK to form the basis of my app.
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.
Is there anyway to achieve the same using the PHP SDK? Or can I set a custom expiry time on the Facebook session?
Extracted from comments
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的...不确定为什么您的会话会出现这样的具体细节 - 我还必须声明,我不太熟悉 PHP 会话,并且没有丰富的经验。
我的建议是让 JavaScript SDK 完成其工作...让它成功“重新检测”您的会话,并在完成此操作后对您的服务器进行 AJAX 调用。在处理该调用的过程中,您可以创建并重新启动 PHP SDK,从而恢复您的会话。
此外,您可以定期调用 FB.getAuthResponse 以确保用户session 仍然有效(至少在 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 :