How to implement Facebook Connect on a PHP website

With 250 million users worldwide, Facebook can’t be ignored. If you have a website where users can log on to, Facebook Connect can help getting your usage numbers increased. Facebook Connect allows for an easy way for users to log on to your site.

A simple Facebook Connect button button will allow the user to log on to your site with their Facebook credentials. They don’t have to register, choose and remember a password, click on a confirm link, … The advantages seem to be clear.

Adding Facebook Connect to your website is not that hard, as a lot of information can be found at http://developers.facebook.com/connect.php. But there are some missing pieces to make it really easy if you have a website running PHP. So let me try to fill in the gaps.

The main steps can be found at http://wiki.developers.facebook.com/index.php/Connect/Setting_Up_Your_Site:

  1. Create an application with theĀ Facebook Developer application. This will provide you with an Facebook Platform API key.
    Follow steps 1-9 and note your public API key and a secret code.
  2. Download the Facebook PHP Client Library from http://wiki.developers.facebook.com/index.php/PHP.
  3. Upload all files in the /php folder to a folder on your webserver:
    • facebookapi_php5_restlib.php, facebook.php, facebook_desktop.php
    • all files in the /php/jsonwrapper subfolder

To use the API in your php-pages, you need to create a new Facebook object with your public AND secret API key:

require_once("../functions/facebook.php");
$facebook = new Facebook('public_key','secret_code');
$fbid = $facebook->user;

$facebook->user is the facebook userid. If this is empty, the user has not yet logged in. You can get show a Facebook login button by using some XFBML-code:

if (!$fbid)
  { ?>
    <p>Log in with your facebook-account:
    <fb:login-button onlogin='window.location="redirect-page";'>
    </fb:login-button>
    </p>
   <? }

For this to work, you need to set up your site. Follow steps 1-5 for the XFBML code to work.

If the facebook userid is not empty, you can get information about the user with the users_getInfo method:

else
  {
    $user_details = $facebook->api_client->users_getInfo($fbid, 'first_name,
pic_square');
    if ( isset($user_details) )
      { ?>
	<p><? if ( $user_details[0]['pic_square'] != '' )
               { ?>
                 <img src="<?=$user_details[0]['pic_square']?>" />
            <? } ?>
           Hello <?=$user_details[0]['first_name']?>, you are logged on
via Facebook.</p>
           <p><a href="#"
onclick="FB.Connect.logoutAndRedirect('/connect/logged_out.php')">log out</a></p>
    <? }
  }

6 Comments

  1. Pingback: Kris’s Box » Blog Archive » Update on How to implement Facebook Connect on a PHP website

  2. I have to admit that your entire web log is very informative. I’ve been spending a lot of free time over the last couple months on the lookout at exactly what is available in accordance with the actual fact that I’m organizing to launch a blog. The material you have place on here is pretty much to the point. It just sounds so unclear with regards to all the technological innovations that are around, but I like the way your seems to be. Gotta adore where technologies has come during the previous 15 years.

  3. it seems like the links provided by you are not available anymore..please come back with some fresh information..i could really use a facebook connect application.
    tnx

  4. Jason,
    Thanks for your comments. The issue with Facebook is that they seem to be changing stuff almost every week.
    I’m not working on a Facebook project right now, but when I do, I will update this post.
    Kris

  5. Will this method work still, I am planning to integrate FB login on my online coloring site. Is there any updated code for the same.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.