如何检查用户是否连接到Facebook? [英] How to check if user is connected to facebook?

查看:123
本文介绍了如何检查用户是否连接到Facebook?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重定向他们连接到Facebook的用户,而不是将他们没有连接到Facebook的用户。怎么做?
我正在使用这个代码从Facebook获取数据。这个代码是在基于Wordpress的页面的第一行执行的。

I want to redirect users they are connected to facebook and not to anoying users they are not connected to facebook. How to do it? I am using this code for aquire data from facebook. This code is executed at first line on my page based on Wordpress .

this_code_php:

this_code_php:

<?PHP

session_start();
    require 'facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'my_number_here',
  'secret' => 'my_number_here',
  'cookie' => true
));

// Get User ID
$user = $facebook->getUser();

if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();

  //echo '$logout '.$logoutUrl;
} else 
{ $loginUrl = $facebook->getLoginUrl();
  //echo '$login '.$loginUrl;
 echo "<script type='text/javascript'>top.location.href = '$logoutUrl';</script>";

 }
?>

连接到Facebook和LOGGING与我的应用程序有区别。
请注意,如果我知道该用户已连接到Facebook(例如已打开的Facebook选项卡),我想自动将他重定向到我的页面与所有的Facebook数据。但我不会打扰其他用户(不是Facebook注册)通过Facebook登录页面。如果他们是连接的 - 好的 - 只需用Facebook数据重定向到我的页面。如果他们不 - 忘记Facebook登录并忘记Facebook数据 - 避免Facebook。问题是行:
echotop.location.href ='$ logoutUrl';;

There is the difference between CONNECTING to facebook and LOGGING to my application. Note, that if I know that user is connected to facebook (e.g. has open facebook tab) I want to automatically redirect him to my page with all facebook data. But I will not bother other users (not facebook registered) by facebook login page. If they are connected - OK - let simply redirect to my page with facebook data. If they are not - forget facebook login and forget facebook data – avoid facebook. Problem is line: echo "top.location.href = '$logoutUrl';";

什么程序?
如果用户连接到Facebook - 程序正在重定向下一步,用户不知道是否采取任何行动。
但是如果用户没有连接到显示用户登录页面的Facebook程序,即使是用户,他们也没有在Facebook上注册。

What program does? If user is connected to facebook - program is redirecting next and user didn't know if any action was taken. But if user is not connected to facebook program displaying user login page, even for user they are not registered on facebook.

我想用以下行替换上面的行:
如果(/ 用户连接 /)echotop.location href ='$ logoutUrl';;

I want to replace line above by this line: if (/user is connected/) echo "top.location.href = '$logoutUrl';";

但我不知道如何检查用户当前是否连接到Facebook?如果可能在PHP或者我必须链接PHP和Javascript,这是很难的。怎么做?

but I don't know how to check if user is currently connected to facebook? If it is possible in PHP or if I have to link PHP and Javascript, which is little difficult. And how to do it?

推荐答案

CBroe是对的。您必须在客户端检查用户状态。
要添加一些细节到CBroe说什么,该文档给出了有用的例子。

CBroe is right. You have to check user status client-side. To add some details to what CBroe said, the document gives you useful example.

FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    // the user is logged in and has authenticated your app
    var uid = response.authResponse.userID;
    var accessToken = response.authResponse.accessToken;
  } else if (response.status === 'not_authorized') {
    // the user is logged in to Facebook, 
    // but has not authenticated your app
  } else {
    // the user isn't logged in to Facebook.
  }
 });

我想将此作为对上一个答案的评论发表,以支持它,但评论表单忽略换行符...

I wanted to post this as a comment to previous answer to support it, but comment form ignores line breaks...

这篇关于如何检查用户是否连接到Facebook?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆