In this article we will learn how to login with Facebook using PHP. Now a day user not feel so good to fill a registration form and prefer to login with social account. This article will help out to create login with Facebook account option for user on your website. This feature in website increases interest of user for registration. So let us see how to implement login with Facebook in web application.
First of all we will see folder structure for files and then how to create Facebook App . After create app we have to put APP ID and Secret Key in config file. Going to discuss simple and easy steps for login with Facebook which are as follows:
The project folder and file structure
src/
facebook.php
config.php
index.php
login.php
logout.php
Follow given below steps:
Step1 : Create a facebook account if you have not please go through given below link.
https://www.facebook.com/
Step 2: For integration of login with Facebook we must have Facebook App. Create Facebook application with given below link and login with your Facebook account login credentials.
https://developers.facebook.com/
Step 3:
Now create new app for login with Facebook as you can see in given below screenshot. Click on create new app link.
Now choose Website option and click on it.
.
Enter name of your new application and click on Create new faceboook App ID.
Choose category for website and click on create Create App ID Button.
Enter your website url and click on next button. Your Facebook App for website is created successfully.
Click on my App and choose your app name and you will redirected to App Dashboard.
Now copy App ID and click on show button to see and copy secret key and you put these in config.php file.
Finally need to enable app to make it live to the public just click on App Review in left side bar and change status for app public.
Your facebook App Creation is completed successfully now move to snippet part. Let us see the PHP snippet.
Step 4: Now upload Faceboook SDK file in src folder as you can see the above structure for folder and file.
Step 5: MySQL Table e.g: table name : users
CREATE TABLE IF NOT EXISTS `users` ( `id` int(25) NOT NULL AUTO_INCREMENT, `name` varchar(100) NOT NULL, `email` varchar(100) NOT NULL, `sid` varchar(100) NOT NULL, `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
Create the table to store information of user logged in with Facebook. Run the given below code in SQL query to create a table.
Step 6: Create a config.php file and write App ID and secret key in it.
<?php include "src/facebook.php"; /*DB Config*/ mysql_connect("localhost","user","password"); mysql_select_db("DB name"); /*FB API Config*/ $app_id = "Your APP ID"; $secret_key = "Your App Secret Key"; $homeurl = "Your site home URL"; $redirecturi = "Redirect URL after Login "; $perm = "email"; //permission to get email $config = array('appId'=>$app_id,'secret'=>$secret_key); //create facebook class object $facebook = new Facebook($config); //get user $facebookuser = $facebook->getUser(); ?> <?php //check for user and define login,logout url if(!$facebookuser) { $loginurl = $facebook->getLoginUrl(array('redirect_uri'=>$redirecturi,'scope'=>$perm)); } else { $logouturl = $facebook->getLogoutUrl(array('next' => $homeurl . 'logout.php')); } ?>
Step 7: Now create index.php file to give Sign in with Faceboook link and it will make API request to Facebook for access user detail when a user click on it.
<?php include('config.php'); ?> <center> <?php if($facebookuser){ ?> <?php echo "Welcome"."<b>".$_SESSION['name']."</b>"; ?><br> <?php echo '<a href="'.$logouturl.'">Logout</a>'; ?><br> <label><b>Your Profile Details</b></label><br> <b>App ID :</b> <?php echo $facebookuser; ?><br> <b>Name:</b><?php echo $_SESSION['name']; ?><br> <b>Email:</b><?php echo $_SESSION['email']; ?><br> <?php }else { ?> <a href="<?php echo $loginurl; ?>" class="fb">Login with facebook</a> <?php } ?>
Step 8: Create login.php file which will work to save Facebook response to database and give access to user to login with Your Facebook Application.
<?php include('config.php'); if($facebookuser) { $users= $facebook->api('/me'); $_SESSION['username'] = $users['name']; $_SESSION['useremail'] = $users['email']; $_SESSION['sid'] = $users['id']; //check for email $ftemail = mysql_query("SELECT * FROM users WHERE `email`='".$_SESSION['useremail']."'"); if(mysql_num_rows($ftemail)>0) { echo $_SESSION['newuser'] = "Welcome back".$_SESSION['username']; } else { $fquery = mysql_query("INSERT INTO users SET name = '" .$_SESSION['username']. "',email='".$_SESSION["useremail"]."',sid='" .$_SESSION['sid']. "'") ; echo $_SESSION['newuser'] = "Thankyou for registering"; } echo "<meta http-equiv='refresh' content='1;url=index.php'>"; } ?>
In this step we will check either user with email id exist in database or not. If it record exist then we will simply welcome for come back and if not then store its record in database and display a message thankyou for register.
Step 9: Create a logout.php to logout the user. It will destroy the session of user and redirect back to home URL for login option.
<?php session_start(); session_destroy(); header('Location: index.php'); ?>
That’s all.
[sociallocker]Download[/sociallocker]
If you loved this article 🙂 please share with your friends and colleagues 🙂 . If you have any query please write in comment.