I'm integrating Facebook with my site and I've added a Logout button whose URL is taken from:
$facebook->getLogoutUrl(array('next' => 'http://mydomain.com/logout.php'));
The problem is that logout.php
is never called. Instead, on click of the Logout button, it redirects to the logged-in user's Facebook home page. It doesn't log the user out of Facebook, and it doesn't call my next
URL.
I noticed that the URL generated by getLogoutURL()
looks like:
https://www.facebook.com/logout.php?next=http://mydomain.com/logout.php&access_token=0
Notice there is an access_token=0. Should that value not be zero? That's the only thing I can think of that might be causing the problem.
I've already set my FB app's Site URL to http://mydomain.com
. While testing locally, I've also edited my hosts
file. I've also googled a lot and I haven't found a solution. The only one that worked was adding an onclick
to my logout button using FB.logout()
. But I would need to use PHP.
Any ideas as to why the logout URL is not working?
When your app uses Facebook Login to authenticate someone, it receives a User access token. If your app uses one of the Facebook SDKs, this token lasts for about 60 days. However, the SDKs automatically refresh the token whenever the person uses your app, so the tokens expire 60 days after last use.
An access token is an opaque string that identifies a user, app, or Page and can be used by the app to make graph API calls. When someone connects with an app using Facebook Login and approves the request for permissions, the app obtains an access token that provides temporary, secure access to Facebook APIs.
To get this token, go to the Graph API Explorer of Facebook. Then, in the User or Page menu, select “User Token” (2). Finally click on “Get Access Token”, you will get a User Access Token.
Okay, I've solved this by creating my own logout URL and adding an access token
$logoutUrl = 'https://www.facebook.com/logout.php?next=http://mydomain.com/logout.php&access_token=' . $facebook->getAccessToken();
I hope you've found a solution! But if not, try with the following code. It worked for me!
$facebook->getLogoutUrl(array('next' =>'http://example.com/logout.php', 'access_token'=>$facebook->getAccessToken()));`
If you check out the official documentation you'll see it says nothing about setting the access token option in the parameter array but it really works! Good Luck!!
The problem is that you need to call getAccessToken BEFORE ask for the logout url.
$facebook->getAccessToken();
Don't need to call it inside as a getLogoutURL parameter. Just call it one line before:
$user = $facebook->getUser();
$access_token = $facebook->getAccessToken();
$logoutUrl = $facebook->getLogoutUrl();
I had the same problem too (access_token=0), but then I realized I was clearing Facebook cookies before calling getLogoutURL(). If you get the getLogoutURL() result first, access_token should not be zero.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With