Access Facebook Session Through PHP

I'm struggling to keep a Facebook session using PHP on my website.

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

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

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

Extracted from comments

It seems that the session expiration time is set to 2 hours, but I'm not sure about that. I do not think that calling a PHP api will matter. I need to clarify a little what is happening. Basically, if you come to my homepage, you will be able to log in via Facebook. If you do, everything will be fine! Once you are logged in and you have authorized the application, this is normal until the session ends. When the session expires, it seems that the PHP SDK cannot determine if you are registered through Facebook, however, the Javascript SDK. I am using getUser () for the PHP SDK.

In other words, since the session has expired, PHP thinks you are no longer logged in via Facebook. The Javascript SDK is able to determine if you are registered or not, regardless of whether the session exists. When he realizes that this is so, he recreates the session in any way. But in order for the session to be picked up by PHP, the page obviously needs to be updated. This is the problem that I have, since the page displays content based on your Facebook login, I need the PHP SDK to be able to recreate the session, so there is no need to refresh the page.

+3
source share
1 answer

Ok ... I'm not sure what the exact details are about why your session works like this: I must also say that I am not very good at PHP sessions and I don't have much experience with them.

My suggestion was to let the JavaScript SDK do its job ... let it β€œre-discover” your session successfully and, after it did, make an AJAX call to your server. When processing this call, you can create and re-initiate the PHP SDK, thereby resuming the session.

Alternatively, you can call FB.getAuthResponse periodically to make sure the user session is still valid (at least in the JavaScript SDK).

From the Fb.getLoginStatus () Documentation :

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

By checking the availability of the authResponse object in the response object, you can be sure that the user is known to your application, and you can start making further calls to the Facebook API. If the authResponse object is not there, the user either is not logged into Facebook or did not allow your application.

0
source

Source: https://habr.com/ru/post/1396502/


All Articles