I’m trying to create a website that allows users to login with their Facebook account. I do not need to make any requests or store any info when they are not on the website and so I do not need a database.
I am only going to accept login with Facebook and no other login types are required. I will need the typical login with Facebook button on the login page and the ability to keep them logged in across multiple pages. I would also like to add an ability to log out.
I’ve downloaded the PHP SDK and I am using the JavaScript SDK together on my pages, but I am having so much trouble.
Is it just me or does the Facebook SDK completely suck?
My first problem is that the JavaScript SDK on the login page doesn’t redirect the user to the main page after authentication. I am manually refreshing the page currently (the PHP SDK realizes that the user is logged in and sends the user to the main page).
Another issue is logout. I am using the PHP SDK’s getLogoutURL() function, and it logs the user out of Facebook, but my access token is still fully valid and PHP refuses to send the user to the login page. Why? I have no idea. Personally, I’d like that the user be logged out of my website and not Facebook.
I’m trying to make a seamless login system with Facebook, but I’m having nothing but trouble! I can’t seem to find any good documentation on Facebook Connect at all. I’m using their examples and problems like this are happening. Maybe they just can’t make a good SDK. I’d like to use the PHP SDK and the JavaScript SDK to make a very nice user experience, but it just doesn’t seem possible.
login.php’s PHP code
require_once("../fb-sdk/src/facebook.php");
$Facebook = new Facebook(array(
'appId' => 'xx',
'secret' => 'xx',
));
$User = $Facebook->getUser();
if ($User)
{
try
{
// Proceed knowing you have a logged in user who's authenticated.
$UserProfile = $Facebook->api("/me");
} catch (FacebookApiException $e) {
error_log($e);
$User = null;
}
}
if ($User)
{
header("Location: index.php");
}
?>
login.php’s JavaScript code
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'xx', // App ID
channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
<div class="fb-login-button">Login with Facebook</div>
index.php’s PHP code
require_once("../fb-sdk/src/facebook.php");
$Facebook = new Facebook(array(
"appId" => "xx",
"secret" => "xx",
));
$User = $Facebook->getUser();
if ($User)
{
try
{
// Proceed knowing you have a logged in user who's authenticated.
$UserProfile = $Facebook->api("/me");
} catch (FacebookApiException $e) {
error_log($e);
$User = null;
}
}
if (!$User)
{
header("Location: login.php");
}
?>
You have to subscribe for login event to get callback and then you can refresh your page. Also for logout you should used your own SESSION of site for login logout beside facebook login, so lets say if you get userid from facebook by calling this function (means user is login with facebook)
Then set something like this in your session, and use this variable to check login status of user with your web.
and when you logout you can simply unset this variable or destroy session.
Here are the links of tutorial which gives you more detail on facebook SDK in PHP / JS
http://thinkdiff.net/facebook/graph-api-javascript-base-facebook-connect-tutorial/
http://thinkdiff.net/facebook/php-sdk-3-0-graph-api-base-facebook-connect-tutorial/