Create A Facebook Login For Your Website Using PHP

Hi all,
A web application the registration form and logins forms are implemented. But nowadays the users are not like to fill the big registration form and they not like to remember and they forget their login details like username and password in each login system. Short registration process helps to get more subscribers for your website. Based on this situation I try to develop the facebook login page integrated into our system using PHP. Everybody has the facebook account, using your existing facebook login details we can connect or login into our application with the help of facebook API system. Login with Facebook is a powerful and quick way to integrate registration and login system into the website. Facebook is the social network and everybody has the facebook account. Facebook Login allow users to sign into your website using their Facebook account credentials without sign up on your website.

In this article, I will explain how to implement user login and registration system with Facebook using PHP and store the user profile data into the MySQL database. For this purpose first, we include Facebook SDK file into our folder.

Facebook Apps Creation

To access the facebook API into our system first we create App ID & App Secret.
Follow the step-by-step guide to creating and configure a Facebook App from the App Dashboard.

    • Go to the Facebook App Dashboard and log in with your Facebook account.
    • Create a new Facebook apps with your desired name (like prittytimes-Test1)
    • 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 clicks on Settings links from the left side menu panel » Click on the Add Platform button » Choose Website category » Enter site URL (http://localhost/my_post/facebook_comment/facebook_login_with_php/).
    • Once you completed the above steps, your apps settings page would something like the below.

  • Now click on Status & Review link from the left side menu panel and make your apps live.
  • Congratulation! your apps creation has completed.

Database Table Creation

If you want to store the facebook user information to our MySQL database, first create users table in PHPMyAdmin with the field. And also we create the database in PHPMyAdmin, under this database we create users table. It’s very important because then only we can insert the data the specified table in MySQL database.

CREATE TABLE `users` (
 `oauth_provider` enum('','facebook','google','twitter') COLLATE utf8_unicode_ci NOT NULL,
 `oauth_uid` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `first_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
 `last_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
 `email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `gender` varchar(5) COLLATE utf8_unicode_ci NOT NULL,
 `locale` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
 `picture` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `link` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `created` datetime NOT NULL,
 `modified` datetime NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Facebook SDK for PHP

To include or create facebook application into our PHP code: first, we include facebook SDK into our directory.
The facebook-php-sdk/ directory contains the latest version of Facebook SDK for PHP

User Class (User.php)

The User class helps to insert or update user data into the database using PHP and MySQL. In User.php file, you only need to specify your MySQL database credentials ($dbHost, $dbUsername, $dbPassword, and $dbName) and table name ($userTbl) where you want to store the user’s Facebook profile information.

Facebook API Configuration (fbConfig.php)

In fbConfig.php page, define Facebook App ID ($appId), App Secret ($appSecret), Callback URL ($redirectURL), and Permissions ($fbPermissions) to connect with Facebook API and working with SDK.

Login & Profile Information (index.php)

When we call index.php file, if the user already logged in his the Facebook account, the profile details would be displayed, otherwise, Facebook login button will appear.

Facebook ID : ' . $userData['oauth_uid'];
        $output .= '
Name : ' . $userData['first_name'].' '.$userData['last_name'];
        $output .= '
Email : ' . $userData['email'];
        $output .= '
Gender : ' . $userData['gender'];
        $output .= '
Locale : ' . $userData['locale'];
        $output .= '
Logged in with : Facebook';
		$output .= '
Click to Visit Facebook Page';
        $output .= '
Logout from Facebook'; 
		$output = '

Some problem occurred, please try again.

‘; } }else{ $loginURL = $helper->getLoginUrl($redirectURL, $fbPermissions); $output = ‘‘; } ?>Login with Facebook using PHP by CodexWorld


Login with facebook


Logout (logout.php)

When the user wishes to log out from their account, the logout.php file will be loaded after log out from Facebook account.

If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

One thought on “Create A Facebook Login For Your Website Using PHP

Leave a Reply

Your email address will not be published. Required fields are marked *