Facebook XFBML登录按钮阻止弹出窗口

时间:2012-01-07 14:22:28

标签: facebook login popup

长篇故事

我目前正在开发一个网站,将其部分内容限制为注册用户(已填写注册表的用户)和Facebook用户(已授权网站访问其信息的用户)。我正在使用cookie来存储用户上次访问该网站时使用的登录类型。 0代表“无先前登录”或“用户已注销”,1代表“注册用户”,3代表“facebook用户”。如果Facebook用户已登录到他们的Facebook帐户,我希望该网站仅在cookie为“2”时登录用户(之前的登录是使用facebook登录完成的)。否则,我希望用户点击“facebook登录”按钮。默认情况下,它会打开一个弹出窗口并立即关闭它。如何阻止弹出窗口显示并将用户发送到特定的URL?

对于那些不理解我凌乱的谈话或只是想达到目的的人

我有一个facebook登录按钮(< fb:login>)而不是弹出窗口,我需要它来重定向用户。

2 个答案:

答案 0 :(得分:0)

使用FB.getLoginStatus()Javascript SDK函数检查用户的登录状态。请参阅:http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/

答案 1 :(得分:0)

使用PHP-SDK:

<?php
require 'facebook.php'; # The SDK

# Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => 'APPID',
  'secret' => 'APPSECRET',
));
# ----------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------

// 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.
    $dt = $facebook->api('/me'); # User Data
    $lk = $facebook->api('/me/likes'); # User like's Data
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

# ----------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------
# Handler for Login Status
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {

  # This is the User LogIn URL, and if the user hasn't already given your app the permissions this url will ask him/her for that.      

  # After the 'scope' is where the permission are written.

  $loginUrl = $facebook->getLoginUrl(array("scope" => "email,user_birthday,user_likes,user_work_history,user_location,user_education_history"));
}
# -------------------------------------------------------------------------------------
if (!$user): header ('Location:'.$loginUrl.''); # This is where we check if the user is logged in to facebook or not, if not, the user will be re-directed to the log in page.
    else: 
    # If the user is logged in, do some code...

    endif;
?>