8

I have the standard Facebook login button on my homepage and I don't want people to automatically log into my site with their Facebook account, only if the user clicks the login button.

If the user is not logged in Facebook, a popup will appear asking him his credentials and he will be redirected to loggedin.html after that.

<div id="fb-root"></div> <fb:login-button perms="email"></fb:login-button> <script> window.fbAsyncInit = function() { FB.init({ appId : 'xxxxxxxxxxxx', status : true, cookie : true, xfbml : true }); FB.Event.subscribe('auth.login', function() { window.location = "loggedin.html"; }); }; (function() { var e = document.createElement('script'); e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; e.async = true; document.getElementById('fb-root').appendChild(e); }()); </script> 

If the user is already logged in Facebook, when he clicks the button the popup appear and disappear right away, I am OK with that. But the user is not redirected to loggedin.html. How can I do that ?

2 Answers 2

1

If you use the PHP SDK, you can use the following code:

require_once 'path/to/facebook.php'; define('A_ID', '*YOUR APP ID*'); define('A_SECRET', 'YOUR APP SECRET'); $facebook = new Facebook(array('appId' => A_ID, 'secret' => A_SECRET, 'cookie' => true)); $userId = $facebook->getUser(); if (!$userId): ?> <!-- HTML to show if the $userId isn't available (user isn't logged in) --> <?php else: ?> <!-- HTML to show if the $userId is available (user is logged in)--> <?php endif; 

Ideally you should use the PHP SDK as it gives you more control over the data and the way it is shown, than using the JS SDK to insert data.

Facebook have a reference for the PHP SDK here: developers.facebook.com/docs/reference/php/

The JavaScript SDK reference is here: developers.facebook.com/docs/reference/javascript/

Sign up to request clarification or add additional context in comments.

Comments

0

I had the same problem. I solved it by using a regular link instead of the thingy and adding a click handler to this href with the facebook FB.login javascript function

http://developers.facebook.com/docs/reference/javascript/FB.login/

Within the different responses I could handle the corresponding Ajax things.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.