I have a (currently very simple) website that is utilising Facebook’s “Website with Facebook Login” app setting. My problem seems simple on the face of it, but I’m unable to work it out, could someone shed any light?
My website homepage is (say) https://www.mysite.net/
– when I log in from here using the code below, it works.
if(!$this->facebook->getUser())
{
//NOT LOGGED IN
$params = array(
'scope' => 'email, publish_stream, user_photos, status_update',
'redirect_uri' => current_url(),
);
$auth_url = $this->facebook->getLoginUrl($params);
$auth_text = "Log In";
}
else
{
//LOGGED IN
$fbid = $this->facebook->getUser();
$fbid = $fbid;
$auth_url = "https://www.facebook.com/logout.php?confirm=1&next=".urlencode(base_url().'logout/')."&access_token=".$this->facebook->getAccessToken();
$auth_text = "Log Out";
}
<a href="<?=$auth_url;?>" class="btn btn-primary"><i class="icon-user"></i> <?=$auth_text;?></a>
.. that works fine.
However, when I use the same code (and the user needs to log in) from say https://www.mysite.net/second_page/, and I use the exact above code on the second page also, the login script redirects to the Facebook Auth Dialog (great), and redirects to the redirect_uri(also great), but the user is NOT logged in to the page – ie, it gives the “Log In” dialog rather than the expected “Log Out” dialog.
Am I doing something wrong here? I would expect the facebook login dialog to work from any page off my app-specified site root. I don’t want to need to redirect my users to the home page for every login, they need to be able to log in from any page of the site.
Thoughts?
Regards,
Paul G
I’ve implemented a workaround. On pages that are NOT the base URL, I’ve had them reference themselves in a session variable, as follows:
Then, the login script will redirect to the app’s base URL. In the base URL, I’ve place a little code checking for the presence of $_SESSION[‘redirect_state’], then kill it, then redirect:
The user is now redirected back to the page they logged in from. Because the FB user is logged in, the redirect session variable will not be set, and if the user heads home, the home page won’t redirect weirdly as, again, the redirect session variable is not set.
I hope this helps someone else out. I’m surprised I haven’t found anyone else talking about this issue, too 🙂