If you are looking for an easy Ajax solution to connect to Facebook, this simple method could do the trick. In this tutorial you will see how we can connect users to Facebook and let them register on your site easily with their Facebook name and email. All these happens without even refreshing the page. I have used jQuery and Facebook PHP SDK files available at Github. But for your convenience, I have created a zipped sample file, downloadable at the bottom of the page, which already includes SDK and jQuery files
There are basically three PHP files in this tutorial, the purpose of these files is to connect to Facebook and import user details, in order to complete registration or login the existing users.
Configuration file (config.php) basically does nothing but stores settings information, which are needed by Facebook API and database queries, we just include this file wherever needed. Main Page (index.php) is the front page where visitors see Ajax Facebook Connect button. Processing (process_facebook.php) is the important file, because it retrieves, stores user details in database, logs-in user and responds with the result.
I am sure at this point, I am sure you must have created a Facebook application and wrote down its App ID and App Secret.
Run MySql query below in phpMyAdmin to create a table called “usertable“, table containing 4 fields. id(Int, Auto Increment), fbid (BigInt, Facebook ID), fullname(Varchar, Full Name) and email(Varchar, Email). Note that fbid is BIGINT to make sure all long facebook IDs fit in it.
Insert needed values in config.php file, replace xxxx with your Facebook App ID, App Secret and MySQL database information. Specify return URL and permissions required.
Main page (index.php) renders “Facebook Connect” button, and logs-in user using jQuery Ajax with click of the button. It uses session variables set in process_facebook.phpto login users, you just have to replace it with some built-in user authentication system, which will instantly check user and log him/her in.
The process_facebook.php file connects to Facebook and compares user information in database table, if connected user information is not available, it registers user using their Facebook data, storing information in the database. In case user information is already available in database, script responses with a welcome back message & logging him in the your website.
process_facebook.php sets PHP session variables to log-in users. You might want to replace the function with your own in-built authentication system to create users or log-in them into your website.
With jQuery connecting to Facebook is super easy, I am sure this will help you make your own Ajax Facebook Connect, any good feedback would be hugely appreciated, Good luck.