Kris’s Box

Some random thoughts of a Training Professional

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>
    <? }
  }

Tags: ,

This entry was posted on Sunday, July 19th, 2009 at 18:45 and is filed under php, Web Development. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

4 Responses to “How to implement Facebook Connect on a PHP website”

  1. September 30th, 2009 at 15:52

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

    [...] few weeks ago, I blogged about my first tests with Facebook Connect. Since then, Facebook has made it a bit easier by introducing the Facebook Query Language, or [...]

  2. May 8th, 2010 at 00:09

    Ai Mestrovich says:

    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. April 27th, 2011 at 13:59

    Jason says:

    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. April 27th, 2011 at 15:46

    Kris Gielen says:

    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

Leave a Reply

Note: the comments are moderated. You will not see your comment immediately, but it should appear in the next couple of hours.

Spam Protection by WP-SpamFree Plugin

Switch to our mobile site