简体   繁体   中英

Get user facebook login status but don't want the redirect

I am building a website that uses the php facebook libraries. At the beginning of every page I am checking to see if the user is logged into facebook or not (or trying to) with the $facebook->getLoginStatusUrl($params) function. The issue I have is that the only real way to check with that function is to have the user redirected to a page based on their statues (by directing them to the url given back by that function). I cant use file_get_contents or cURL because the URL returned by that function uses https. Is their anyway I can get the contents of that URL with a server-side method? Or is the absolute only way to do this with a redirect? I would much prefer not to make this a client side action (via Javascript). Any ideas?

我对Facebook的PHP库不是很熟悉,因此无法确定是否有更好的方法来完成您要尝试的工作,但是file_get_contents和cURL都可以在HTTPS上正常工作,只需为其提供完整的URL (例如“ https://google.com/”)

You can use the facebook getUser() function to check if the user is logged and has given permisssions to your web. After that you can use getLoginUrl() or getLogoutUrl() to show the user the right button:

$user = $facebook->getUser();    

if (!$user)
{
  $loginUrl = $facebook->getLoginUrl(array(
    'redirect_uri' => REDIRECT_URL,
    'scope' => APP_PERMS,
    'display' => 'page'
  ));

  //do something with $loginUrl, for example:
  echo '<a href="'.$loginUrl.'">Login with facebook</a>';
}
else
{
  //check that this user is still valid
  $realUser = $facebook->api('me');

...

You can learn more about this functions in facebook docs:

https://developers.facebook.com/docs/reference/php/

Is their anyway I can get the contents of that URL with a server-side method?

That won't help you, because it's not your server that the client uses to log in to Facebook, but his browser.

And therefor the check if the user is logged in must happen in their browser – that's why the browser gets redirected to Facebook, because this is the only way to read the cookies that are set under Facebook's domain.

I would much prefer not to make this a client side action (via Javascript).

It already is kind of a client-side action, because of the redirect.

There is absolutely no way to have your server alone figure out if the user is logged in to Facebook in their browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM