0
votes

The long story

I'm currently working on a site, that restricts some of its content to registered users (users, who have filled the registration form) and facebook users (who have authorized the site to access their informatin). I'm using a cookie to store which type of login was used the last time the user was on that site. 0 stands for "no previous logins" or "user logged out", 1 for "registered user" and 3 for "facebook user". If a facebook user has logged in to their facebook account, I want the site to log the user in ONLY if the cookie is "2" (the previous login was done using facebook login). Otherwise, I want the user to click "facebook login" button. By default, it opens a popup and closes it immidietly. How can I prevent the popup from showing up at all and sending the user to a specific URL instead?

For those who don't understand my messy talk or just want to get to the point

I have a facebook login button (<fb:login>) and instead of a popup, I need it to redirect the user.

2

2 Answers

0
votes

Use FB.getLoginStatus() Javascript SDK function to check the login status of users. See: http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/

0
votes

Use the 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;
?>