Hi im new to the facebook api and have managed to get a user to login using facebook, however I am trying to make a logout that redirects to a different page eg. logout.php The issue is when it logs out it redirects to the same page.
The code im using to get the logout link is:
$logoutUrl = $facebook->getLogoutUrl();
How can i make it so the logout url automatically logs you out of facebook and redirects you to logout.php?
Thanks in advance!
UPDATE
An example of the url i currently get from the getLogoutUrl() function:
https://www.facebook.com/logout.php?next=http%3A%2F%2FMYDOMAINHERE.com%2F&access_token=MYACCESSTOKENHERE
My guess is that if i can pass it a redirection URL to autogenerate with that function it would look something like this:
https://www.facebook.com/logout.php?next=http%3A%2F%2FMYDOMAINHERE.com%2FLOGOUT.PHP%2F&access_token=MYACCESSTOKENHERE
UPDATE
Never mind i just figured it out. Works the same way the login does with the
getLogoutUrl(array(
'next' => 'http://' . $_SERVER['HTTP_HOST'] . '/logout.php'
));
Do you mean this?:
header ("Location: $logoutUrl")
exit;
Related
I have created a facebook login using the tutorial from http://www.9lessons.info/2011/02/login-with-facebook-and-twitter.html. But the problem is after logging in the user will be redirected to the index.php page. How can i redirect him to the page from which he has clicked the login.
I tried saving the previous url to session. But i don't know why, its not working. It worked for normal login, but not for facebook. I also tried saving the previous url to cookie. It also didn't work. Url is saving, But after logging in session url/cookie url is lost. Can someone pls tell me an alternative method???
Example to store the current page
Do on all but your login page before redirect:
session_start();
$_SESSION['lastpage'] = $_SERVER['HTTP_REFERER'];
On all other pages :
session_start();
if(isset($_SESSION['lastpage'])) {
$lastpage = $_SESSION['lastpage'];
$_SESSION['lastpage'] = false;
unset($_SESSION['lastpage']);
header("location: " . $lastpage);
}
Something like that should work. Make sure to santize the $_SESSION variable, and also validate the last URL was from your site (or supported site) before setting the session var.
We have Joomla bases site - a given url to particular page works fine when user is logged in.
If user is not logged in, the url redirects to login page and stays there.
How to redirect the browser to load the intended page after login??
At the place where you create the url to do the redirect append the return parameter to it. Something along the lines should work:
$return = base64_encode(JURI::getInstance(
JFactory::getURI()->toString(array('path', 'query', 'fragment'))
));
JFactory::getApplication()->redirect(
JRoute::_('index.php?option=com_users&view=login&return=' . $return, true, true)
);
I have visited this link but cant get any help
$facebook->getUser() ALWAYS Returning ID - Can't Logout
the problem is when $facebook->getLogoutUrl is executed it just logout the user from facebook means destroy the facebook session but data return by the app is still there on the page. how to clear that data.
what i want is when logout link is clicked than the data on my app page also clear and login with facebook link appears.
here is the link to my example page
http://www.webdesigncc.info/demoz/myfacebook/examples/example.php
This may solve your problem
function facebook_logout()
{
$logout = $facebook->getLogoutUrl(array('next'=>'your url to be redirected after logout'));
$facebook->destroySession(); /*To destroy facebook sessions.
Available in facebook php sdk*/
$session_destroy(); //Optional if you want to destroy your site's sessions
header("Location:$logout");
}
Using the following way you can clear all datas.
public function destroySession() {
$this->setAccessToken(null);
$this->user = 0;
$this->clearAllPersistentData();
}
I am using a modified version php-sdk version 3.0.0 sample code at github.com/facebook within the CodeIgniter framework as a helper.
My problem is just as the title says: When I click the logout anchor (provided by $Facebook->getLogoutUrl()) I am redirected back to the same page and receive an OAuthException:
Fatal error: Uncaught OAuthException: Error validating access token: The session is invalid because the user logged out. thrown in [...]/base_facebook.php on line 959
When I refresh, it loads the "login" anchor like it normally would. What is happening on that refresh/post-back that isn't happening on that initial redirect?
I realize this is limited information but due to the problem I think it may be a simple fix.
EDIT: This post seems to be relevant: http://forum.developers.facebook.net/viewtopic.php?id=71219
Specifically this line:
setcookie('fbs_'.$facebook->getAppId(), '', time()-100, '/', '.domain.com');
However, I am not sure how to implement this and still use $facebook->getLogoutUrl();.
Thanks in advance and just let me know if more information is necessary.
I was having the same problem and nearly pulling my hair out. However, after some research, it appears the problem is an offending cookie. This line on logout should fix it:
setcookie('fbs_'.$facebook->getAppId(), '', time()-100, '/', '.domain.com');
Ensure to add the '.' before the domain name if subdomains are being used.
I hope this helps!
As suggested, I tried:
setcookie('fbs_'.$facebook->getAppId(), '', time()-100, '/', '.domain.com');
This didn't work. What I did, was to just copy from the fb example code:
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
The middle part, with if try get user_profile, is a test to get the user profile, and if it fails the userid will be unset. This will make the last part with getLoginUrl() and getLogoutUrl() correct.
I do believe setting the cookie is more correct, than to try a request and see if it fails... but since the setcookie didn't work, I didn't have a choice :(
Stupid facebook that returns a token with this $user = $facebook->getUser();, when the user actually is logged out.
Hope this helps those who is in need.
From the looks of your error it would appear your website is still trying to connect to Facebook using the SDK. When you run the logout function provided by Facebook make sure to clear whatever sessions or storage you have that triggers calls to Facebook.
It's likely that they aren't being cleared before you attempt your Facebook logout, and this is why it still thinks you have a connection but then works fine on refresh.
What I ended up doing was this:
$facebook->getLogoutUrl(array('next' => site_url('logout')));
Then in the 'logout' controller:
$_SESSION = array();
$this->load->view('myoriginalview');
On logout, the facebook logout url's query string redirect_uri value is set to redirect to the 'logout' controller which then clears the session and loads the view on which the logout button existed in the first place. Everything functions fine. Now I just have to figure out how to handle an expired session as opposed to a logged out user -_-
EDIT:
What I've done now is invalidate the cookie in the proper manner as described on the facebook developers forum. I really wish their documentation was better and described this for their PHP SDK.
I'm doing some integration with Facebook on a project (using graph api) and everything was working fine until now I discovered that facebook doesn't clear my session when I click on the logout url. I'm doing logout through php, not javascript, so the logout url looks like:
https://www.facebook.com/logout.php?next=url&access_token=token
After clicking on that link the user is logged out on facebook, but the session still exists on my website. To actually clear the session I have to refresh the page one more time after clicking that url. This is a strange behavior, in my opinion.
What you guys think of this? I tried even to personally remove the facebook cookie, but it is still there, and it is cleared only after I hit the refresh button on my browser.
p.s. to get the facebook session I do something like this:
My_Facebook_Helper::instance()->getSession(); //it should be === null if it doesn't exist
p.p.s. it could be a bug? i don't remember having this issue about a week ago when I first started to implement this
I ran into this bug and realized it wasn't that the session wasn't being cleared, but if you have offline access, it will automatically get you a new session.
Try finding the formatData function somewhere at LoginWindow (AS3) and find this line:
vars.redirect_uri = FacebookURLDefaults.LOGIN_SUCCESS_URL
Change the value for 'http://www.facebook.com/' and logout from that html page when logged in.
This is a temporary solution to logout if you are developer, not the end user.
This is registered as a bug. Please add your own repro to this bug to help get it fixed
http://developers.facebook.com/bugs/250825644953332
I have just a solved a simular problem today.
Try using this to get your session:
$session = $facebook->getSession();
And this could also help for the logout url:
$logoutUrl = $facebook->getLogoutUrl(array('next' => $url, 'session_key' => $session['session_key']));
Hope it helps!
I tried this
$logoutUrl = $facebook->getLogoutUrl(array('next' => 'some url', 'session_key' => $session_key)) . 'session_key=null';