A lot of sites now use OpenID. This is great, as you can use the one account on multiple sites. Unfortunately, Facebook accounts can not yet be used as OpenIDs :(. However, using Facebook logins isn't too hard, as they support using OAuth 2.0. OpenID and OAuth are fundamentally for different things (OpenID is authentication, OAuth is authorization), but it still works well in this situation.

Firstly, sign up for a Facebook application at the Facebook developer website. You'll have to correctly set the site URL and site domain. Copy the application ID and application secret as shown on the Web Site section of the settings, as you will need them later.
Facebook application details

Now we're ready to begin. Here's a very simple class for logging in via Facebook. It doesn't have much error checking, but should work okay: Download the class (Facebook.php). Here's some code that uses that class:

$facebook = new FacebookLogin('100929283281389', '8*******************************1');
$user = $facebook->doLogin();
echo 'User\'s URL: ', $user->link, '<br />';
echo 'User\'s name: ', $user->name, '<br />';
echo 'Full details:<br /><pre>', print_r($user, true), '</ pre>';

The first number in the constructor is the application ID, and the second one is the application secret (remember these from earlier? Here's where you use them!). Stick both the class and the little code snippet above into a .php file, and access it. If everything works correctly, you'll be able to hit that file to log in via Facebook, and get the user's details after logging in. Here's a demo to show you how it works. The idea now is you save the file as something like "FacebookLogin.php", and add a "Log in using Facebook" link on your site that goes to it :).

The class I've provided here is just a base class that you can base your own code on. What you do now is up to you. Here's some suggestions:

  • Move the application ID and secret into a config file, instead of hard-coded like above
  • If you're using this to log in to a site, I'd store some of the user's details (like name, URL and Facebook ID) in session variables.
  • Maybe do things like load the user's profile picture. The access token retrieved at this line: $this->access_token = $result_array['access_token']; can be used to access pretty much anything on Facebook, as long as the user has given permission. Take a look at the demo to see what info you can get by default

Good luck! :)

Tags Facebook Connect, login, oauth, openid, PHP

Short URL for sharing: http://dan.cx/B4y. This entry was posted on Friday, 10th September 2010 at 7:01 PM and is filed under Facebook, PHP, Programming, Web Development. You can leave a comment if you'd like to, or subscribe to the RSS feed to keep up-to-date with all my latest blog posts!

13 comments

  1. Avatar for Best Friend said:
    The link http://stuff.dan.cx/facebooklogin_test doesn't seem to work
    1. Avatar for Daniel15 said:
      Sorry about that, the correct URL is http://stuff.dan.cx/facebook/login_test/. I'll update the post :)
  2. Avatar for grek said:
    Hy facebook dont return user email ?
    1. Avatar for Daniel15 said:
      It's a permission you need to use - See the Facebook documentation at http://developers.facebook.com/docs/authentication/permissions/ (it's the "email" permission).
  3. Avatar for Rajesh said:
    Hello Dan,

    I want to auto login on facebook from my site i have put username and password into database of facebook account when user click on facebook button then he will redirect on facebook .


    Give me suggition..................
    1. Avatar for Daniel15 said:
      You cannot log in with a Facebook username and password. You MUST use OAuth for logins.
  4. Avatar for Marky said:
    I have to try this out some time. Was looking for a way not to login to like 10 different accounts separately(mail,blog,etc) but with a single login. No I don't want to use roboforms or store my password through browser. I used Google and Facebook too often so it's either of the 2. Or maybe openID? What would you recommend? Thanks! :D
    1. Avatar for Daniel15 said:
      If you use Facebook a lot, it might be a good idea to use Facebook to log in to all your sites. Just be careful to keep your password secure! Between Google and Facebook, I'd choose Google. I personally like OpenID the best, though. :)
  5. Avatar for Abhay.. said:
    Hi ALL,
    I want to use facebook login in my .Net website.
    Like i will login with facebook credentials in my website and can do anything throughout application.
    Please suggest...
    Thanks
    Anshu
    1. Avatar for Daniel15 said:
      You could try write some similar code for .NET. But there's a Facebook API implementation for .NET that might help you. Take a look at http://facebooksdk.codeplex.com/ :)
  6. Avatar for Ashish said:
    I want email id too from user but I am not sure how to edit your existing code. Can you please let me know what needs to be changed for getting email id access?
    1. Avatar for Daniel15 said:
      You need to request the "email" permission. See https://developers.facebook.com/docs/reference/api/permissions/

      In my code, you'd find this bit:

      $data = array(
      'client_id' => $this->client_id,
      'redirect_uri' => $this->my_url,
      'type' => 'web_server',

      And add this under it:

      'scope' => 'email',
  7. Avatar for james said:
    i want logout code please help me

Leave a comment

Cancel reply

(required)

(required, but will not be published)