Nowadays users are not interested to filling the big registration forms. This tutorial helps you to allow users to login at your site without registration. Facebook login is very quick and powerful, sure this helps you to increase your web project registrations. This tutorial explains how to implement user registration and login with Facebook using PHP and store the user information’s to the MySQL database. We have used Facebook PHP SDK with Facebook Graph API for build Facebook login system using PHP.
In our step by step tutorial, at first we will describe the folder structure of this project. After that you will learn how can you create the Facebook apps and use this app for Facebook login at your website. And finally we will discuss about the integration process of our scripts. This tutorial helps you to make whole process quicker and easier.
You can check the live demo of Facebook login with PHP from the above Demo link and also able to download the complete project from the above Download link.
This project has three folders named
images/ and some PHP files. The folders and files structure are given below:
Facebook apps creation:
Go to the https://developers.facebook.com/apps/ and login at your Facebook developer account.
Create a new Facebook apps with your desired name like Web Login.
If you want to test Facebook login at the localhost server, then your App Domains should be localhost. Also localhost domain will only work, once you add platform. For add a platform click on Settings link from the left menu section at the apps page => click on the Add Platform button => choose Website category => enter site URL (http://localhost/facebook_login_with_php/).
Once you completed the above steps, your apps settings page would be displayed like the below screenshot.
Now click on Status & Review link from the left menu section and make your apps as live. Contact email is required for enable the apps live option. If you have not add apps contact email earlier, go to the settings page and add email. Once you submit the contact email, you would be able to enable the apps live option.
Congratulation! your apps creation has completed.
Database table creation:
To store the user information, you need to create a database and a table. At first create a database like "codexworld". After that copy the below SQL query and run the SQL query on the database.
CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `oauth_provider` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `oauth_uid` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `fname` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `lname` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `gender` varchar(10) COLLATE utf8_unicode_ci NOT NULL, `locale` varchar(10) COLLATE utf8_unicode_ci NOT NULL, `picture` text COLLATE utf8_unicode_ci NOT NULL, `created` datetime NOT NULL, `modified` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
In this project you just have to modify only two files.
Database configuration (
functions.php file have
Users class with some functions. Into the
__construct() function we have configured the database and connect with the database. You need only to change the
$dbName variables value with your MySQL server details.
$dbServer = 'localhost'; //Define database server host
$dbUsername = 'root'; //Define database username
$dbPassword = ''; //Define database password
$dbName = 'codexworld'; //Define database name
$con = mysqli_connect($dbServer,$dbUsername,$dbPassword,$dbName);
die("Failed to connect with MySQL: ".mysqli_connect_error());
$this->connect = $con;
Facebook API configuration (
config.php): open the
config.php file and modify the
$homeurl variables value.
Note: You can find the
App ID and
App Secret from your Facebook Apps settings page.
include_once("inc/facebook.php"); //include facebook SDK
######### Facebook API Configuration ##########
$appId = 'Insert Facebook App ID'; //Facebook App ID
$appSecret = 'Insert Facebook App Secret'; // Facebook App Secret
$homeurl = 'http://localhost/facebook_login_with_php/'; //return to home
$fbPermissions = 'email'; //Required facebook permissions
//Call Facebook API
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret
$fbuser = $facebook->getUser();
If you have any query about this tutorial and scripts, feel free to comment here.