Other sites run by Goldstein Technologies: RealFoot | QGenix | ZingJet | SkyDroid | Paul on Golf |

Facebook Connect Tutorial

Contents

Create New Account Page

This page will create a new account on your site based on some user information from Facebook. Depending on the requirements for your site, you may have to ask the user to provide some additional information if what Facebook provides is not enough.

In fact Facebook doesn't really let you store all that much (see these guidelines), but really you mostly just need thier Facebook id. There are other ways to get user information, you are just not allowed to store it. The users_getInfo() php call is an example of how to do this. We can at least store their proxied email address using this call.

Implementation

Step 1

We will go through our usual method of getting $fb_user, and then make a PHP call to get some additional user info.

include_once 'site/fbconnect/config.php'; //has $api_key and $secret defined.
include_once 'site/facebook-platform/client/facebook.php';
global $api_key,$secret;
$fb=new Facebook($api_key,$secret);
$fb_user=$fb->get_loggedin_user();

$user_details=$fb->api_client->users_getInfo($fb_user, array('last_name','first_name','proxied_email'));  
$firstName=$user_details[0]['first_name']; 
$lastName=$user_details[0]['last_name'];
$email=$user_details[0]['proxied_email'];

Step 2

You can insert the storeable information into your database. Chances are you may have some other required fields that you would like to fill in. You could prompt the user to enter these where applicable. You may also have to alter your database to allow some fields to be null (e.g. they won't need a password if they are logging via Facebook Connect).

I'll just store their Facebook ID and their proxied email address.

$dbconn=connectdb();
$query="insert into userprofile (fb_userid,fb_proxy_email,created_ts) values ('$fb_user','$email',sysdate())";
mysql_query($query,$dbconn);
closedb($dbconn);

I'm sort of assuming that your userprofile table has an auto increment key that you don't have to specify. This would be your sites internal user id field.

Step 3

Now that you've entered a record into your database they should be all set to go. Of course you still have to log them into your site officially and set any session values that you'll need. One way to do this is to forward them off to your Login page (or call some login() function that you may have defined).

I'll justforward them off to the Login page after a brief message. The Login page will take care of querying the database with their Facebook Id and then setting any session values and will then forward them off to the Logged in User page. This may not be the most efficient technique but its easy.

<html>
	<head>
	<title>Registration Complete</title>
	<meta http-equiv="refresh" content="5;url=http://www.yoursite.com/login_page.php" />
	</head>
	<body>
	<h2>Thank you <?php "$firstName"?>, your registration is complete</h2>
	<p>We have created a new account for you using Facebook Connect</p>
	<br/>
	<p>Logging you in now...<a href="login_page.php">Hang on</a>...</p>
	<br/>
	<br/>
	</body>
</html>

That's about it.

Next

The conclusion page will wrap things up.