Facebook php SDK getLogoutUrl() problem
Asked Answered
D

2

4

When i want to logout users from my website i use:

$logoutUrl = $facebook->getLogoutUrl(array('next' => 'logout.php'));

And $logoutUrl displays correct link, however it's not redirecting me to the url specified in next. It redirects me to the page that started logout.

As it looks that there is very much articles on internet, but they all use same methods and for many people those don't work. How to properly logout user from facebook and then perform my regular logout script?

EDIT: This worked but still want some non-javascriptSDK based logout.

<a id="logout" href="logout.php" onclick="FB.logout(function(response) { window.location = 'logout.php' }); return false;" title="<?php echo $lang['logout']; ?>"><?php echo $lang['logout']; ?></a>
Diandrous answered 21/1, 2011 at 15:42 Comment(0)
Y
10

You should use absolute URLs. e.g.

//   (or https://)
$here = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$next = preg_replace('~#.*$~s', '', $here);
$next = preg_replace('~\?.*$~s', '', $next);
$next = preg_replace('~/[^/]*$~s', '/logout.php', $next);
$logoutUrl = $facebook->getLogoutUrl(array('next' => $next));

Or simply:

$logoutUrl = $facebook->getLogoutUrl(array('next' => 'http://...../logout.php'));
Ylangylang answered 21/1, 2011 at 16:51 Comment(0)
I
0

I know this is old, but the reason getLogoutUrl() isn't redirecting to your "next" url is because it doesn't log the user out or redirect at all. It simply gives you the proper url which you need to use to do the logout and redirect (e.g. header("Location: $logoutUrl")). After you redirect, the user will be logged out and your "next" url will be called.

Note: do not clear out the Facebook session variables (e.g. destroySession) before calling getLogoutUrl(). If you do, the access token included in the returned url will equal 0 instead of your access token.

The documentation isn't too clear on this, but the function is very appropriately named.

It answered 16/5, 2013 at 18:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.