Facebook Connect PHP SDK - Differents accounts - php

I'm using Facebook Connect (PHP SDK) in my website.
I have 2 kind of users to sing in new user in my website (Premium User and Normal User) and I want to offer Facebook Login to both insted fill new form.
If the user login with Facebook, but doesn't have a record in database, I have to do it, using its Facebook email. Premium Users are recorded in table1 and Normal Users are recorded in table2.
The question is: how can I dettect what user logged with Facebook to check if it's already recorded in its table (in Facebook returning page)?
Just to clarify: I have two forms (one for Normal User and another to Premium User), and each form has a Facebook Loggin button.
Thanks.

Question solved!
require "facebook/src/facebook.php";
$facebook = new Facebook(array(
"appId" => "xxxxxxxxxxxxxxxxxxxx",
"secret" => "xxxxxxxxxxxxxxxxxxxx",
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api("/me");
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}else {
$facebook_url_user_premium = $facebook->getLoginUrl(array(
"scope" => "email",
"redirect_uri" => "https://URL"
));
$facebook_url_user_normal = $facebook->getLoginUrl(array(
"scope" => "email",
"redirect_uri" => "https://URL"
));
}
So you call the correct $facebook_url.... variable in each Facebook Login button.

Related

Facebook application connection doesn't work

I have a problem with my logging into my Facebook application.
When I click "login" I get redirected to Facebook to accept the permissions then Facebook redirects me back. But it doesn't change anything. The URL down is dynamic generated. When I try the same script on an other URL that's not dynamic it's work.
Here is the URL there i try to perform a login.
http://www.testaiq.se/test_592.html
What could be the problem? PHP is behind the URL over here.
require_once("facebook/facebook.php");
// Creating our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'XXX',
));
// Getting User ID
$user = $facebook->getUser();
// Get Access token
$access_token = $facebook->getAccessToken();
// 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.
// Retrieving user's friend list using fb graph api
$user_profile = $facebook->api('/me','GET');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if (!$user) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_likes'
));
}

Ask for Facebook permissions with dialog box only

This code is used to get facebook data from users of my codeigniter application that I would be launching in a facebook page tab.
require_once('facebook-php-sdk/src/facebook.php');
$facebook= new Facebook(array(
'appId' => $this->config->item('app_id'),
'secret' => $this->config->item('app_secret'),
'allowSignedRequest' => false,
));
$_REQUEST += $_GET;
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
} catch (FacebookApiException $e) {
$user = null;
}
} else {
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email'
));
redirect($login_url, 'refresh');
}
I want it so that if the user is already logged into facebook that when they open the tab to use the app, instead of being asked to log in, that the dialog box come up asking for permissions. I need email permissions.
How do i skip the login to get permissions step if a user is already logged into Facebook, and just have the dialog box appear asking for permissions?

Ask permission for Facebook user

I am using the following code to get Data on the logged Facebook user for my codeigniter application.
require_once('facebook-php-sdk/src/facebook.php');
$facebook= new Facebook(array(
'appId' => $this->config->item('app_id'),
'secret' => $this->config->item('app_secret'),
'allowSignedRequest' => false,
));
$_REQUEST += $_GET;
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
} catch (FacebookApiException $e) {
$user = null;
}
} else {
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email'
));
echo 'Please login.';
}
This works and returns me the user's Facebook data, and if no user is logged in it provides the login link.
In the login link I ask for email permission as I need to collect these for communication. When the user logs in with the login link in the else clause, I successfully get the user's email address.
However I want to launch this application in a Facebook page tab using Woobox. Therefore the user would usually always be logged into their account. But the login to get email permissions is in the else statement if the user isn't logged in.
How do I ask for email permissions for an already logged in user, without haveing them login again?
How can I make it so that if the user is already logged into facebook, that the auth box asking the user for permissions comes up, where they can click ok to continue and authorize, rather than the login button.

Facebook Application get user information

i am trying to write a Facebook application where once installed on a page provides a simple Form once this form is populated it would create a new table with this users username or userid.
So far i have the FBML form up and running using an external php file to process it all how can i get users username or userid to my forms php action file.
Are you using the PHP SDK or the javascript SDK? If you're using the PHP SDK, it's as simple as this:
require_once("facebook.php");
$facebook = new Facebook(array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
));
$user = $facebook->getUser(); // This is the FB user ID
$username;
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
$username = $user_profile['username']; // this is the facebook username.
// If the user hasn't set it manually, it will be something
// like "john.doe.50"
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}

Facebook PHP SDK, post as user 1 while logged on user 2

My current code:
(... variables setting, etc.)
$facebook = new Facebook(array(
'appId' => $apiid,
'secret' => $secret,
'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
if ($session)
{
(do some stuff on user's 1 page)
}
else
{
$login_url = $facebook->getLoginUrl();
header("Location: ".$login_url);
exit;
}
I tried once, it worked. I was logged on user 1 to delete post ASAP. Then I've logged on user 2 account and tried this code. I was redirected to user's 2 facebook wall. What to do now? Do I need some extra permissions?
"Posting while user A" whilst logged in as User B kind of defeats the whole purpose of authentication. What you have described is likely impossible. You probably could post as User A on to User B's wall, but you cannot post as User B unless you are authenticated as them.

Categories