Facebook has recently released PHP SDK version 4, you can read new article here about this SDK and how to implement it in projects like this.
I have created 4 files in this tutorial, first one is config.php, this file stores Facebook app ID, app SECRET and database information needed by other files. Second one is the index.php, front page for user interaction where visitors see Facebook Login button, from this page ajax requests will be sent to server. The process_facebook.php runs in the background connects to Facebook to fetch user data and inserts records in database. channel.php is included to improve communication speed in some older browsers as described here.
Create User Table
Run MySql query below in phpMyAdmin to create a table called “usertable“, table contains 4 fields. id(Int, Auto Increment), fbid (BigInt, Facebook ID), fullname(Varchar, Full Name) and email(Varchar, Email).
CREATE TABLE IF NOT EXISTS `usertable` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`fbid` bigint(20) NOT NULL,
`fullname` varchar(60) NOT NULL,
`email` varchar(60) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
To check whether user is logged in or not in this tutorial, I’ve created session variable called $_SESSION[‘logged_in’], if you are using some user management system in your website, you have to make appropriate changes here.
That’s it! I am sure this will help you make your own Ajax Facebook Connect, Good luck.