Hello friends, welcome PHP Facebook Login Tutorial. In this tutorial we will create a PHP Facebook Login feature in your webpage.
PHP Facebook Login – Video Demo
If you liked the video or want to create a Facebook Login Feature to your web app then lets start.
The first thing we need to do is create an app in https://www.developers.facebook.com
Creating a Facebook App
- Go to facebook developers and create a new app
- You will get an App Id and an App Secret after creating the app.
- Once you have created your app in facebook developers, go to your app’s dashboard
- Click on settings
- Click on add a platform
- Select website
- Because I will be using wamp server for this demonstration I have given site url as http://localhost.
- And App Domain will also be localhost.
- Fill the values and click save changes.
Setting Up Facebook SDK
- Inside www folder of your wamp server directory, create a new directory.
- I have created FacebookLoginPHP inside my www folder.
- Inside that directory create a new directory named lib.
- Now you need to get the Facebook SDK for PHP.
- Download the facebook-php-sdk-v4-4.0-dev zip file and extract it.
- Inside the extracted folder go to src folder.
- Inside src folder you will find Facebook folder, copy this folder and paste it inside your lib folder which you had created.
- Now thats it, your SDK is configured, lets move to coding part now.
Creating a PHP Facebook Login
- Inside your project directory create a file named index.php.
- Open the file to your text editor (I am using notepad++).
- First we will define some constants that will be used for login.
1 2 3 4 5 6 7 |
<?php define('APP_ID', 'YOUR APP ID'); define('APP_SECRET', 'YOUR APP SECRET'); define('REDIRECT_URL','http://localhost/FacebookLoginPHP/'); ?> |
- Fill the app id and app secret which you have already created.
- In REDIRECT_URL you need to provide the address where you will be navigated after login attempt.
- In this case we will be redirected to the same index.php file.
- Now we will create the basic html structure for our document.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php define('APP_ID', 'YOUR APP ID'); define('APP_SECRET', 'YOUR APP SECRET'); define('REDIRECT_URL','http://localhost/FacebookLoginPHP/'); ?> <!DOCTYPE html> <html> <head> <title>Facebook Login Sample</title> </head> <body> </body> </html> |
- Inside body we will write our php code.
- First we will include the SDK
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php /* Author: Belal Khan website: www.simplifiedcoding.net */ //INCLUDING LIBRARIES require_once('lib/Facebook/FacebookSession.php'); require_once('lib/Facebook/FacebookRequest.php'); require_once('lib/Facebook/FacebookResponse.php'); require_once('lib/Facebook/FacebookSDKException.php'); require_once('lib/Facebook/FacebookRequestException.php'); require_once('lib/Facebook/FacebookRedirectLoginHelper.php'); require_once('lib/Facebook/FacebookAuthorizationException.php'); require_once('lib/Facebook/FacebookAuthorizationException.php'); require_once('lib/Facebook/GraphObject.php'); require_once('lib/Facebook/GraphUser.php'); require_once('lib/Facebook/GraphSessionInfo.php'); require_once('lib/Facebook/Entities/AccessToken.php'); require_once('lib/Facebook/HttpClients/FacebookCurl.php'); require_once('lib/Facebook/HttpClients/FacebookHttpable.php'); require_once('lib/Facebook/HttpClients/FacebookCurlHttpClient.php'); |
- Now we will use the namespaces.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//USING NAMESPACES use Facebook\FacebookSession; use Facebook\FacebookRedirectLoginHelper; use Facebook\FacebookRequest; use Facebook\FacebookResponse; use Facebook\FacebookSDKException; use Facebook\FacebookRequestException; use Facebook\FacebookAuthorizationException; use Facebook\GraphObject; use Facebook\GraphUser; use Facebook\GraphSessionInfo; use Facebook\HttpClients\FacebookHttpable; use Facebook\HttpClients\FacebookCurlHttpClient; use Facebook\HttpClients\FacebookCurl; |
- For using login with facebook we need to start session.
1 2 3 4 |
//STARTING SESSION session_start(); |
- Now we need to set our application using app id and app secret.
1 2 3 |
FacebookSession::setDefaultApplication(APP_ID,APP_SECRET); |
- Create an object of the FacebookRedirectLoginHelper class, it will take a parameter of a string.
- This is string is the URL where you will get redirected after login attempt.
1 2 3 |
$helper = new FacebookRedirectLoginHelper(REDIRECT_URL); |
- After redirection we will get the session using our helper object.
1 2 3 |
$sess = $helper->getSessionFromRedirect(); |
- If $sess is not null that means we have logged in successfully, else log in is unsuccessful.
- We can check this using simple if else.
1 2 3 4 5 6 7 8 9 10 |
if(isset($sess)){ $request = new FacebookRequest($sess, 'GET', '/me'); $response = $request->execute(); $graph = $response->getGraphObject(GraphUser::className()); $name = $graph->getName(); }else{ echo '<a href="'.$helper->getLoginUrl().'">Login with Facebook</a>'; } |
- So if our $sess is not null we will get the user name of the account.
- Else we will see a link saying login with facebook.
- You can do some CSS to make it look better.
- My final code with some CSS effect is.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
<?php define('APP_ID', '823531574391190'); define('APP_SECRET', 'c83f652eb093f3768f09ca5ebbdd7f03'); define('REDIRECT_URL','http://localhost/FacebookLoginPHP/'); ?> <!DOCTYPE html> <html> <head> <title>Facebook Login Sample</title> <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css"> <style> body{ width:100%; } .facebooklogin{ font-size:100%; font-family:arial; display:block; width:200px; height:20px; background-color:#142dac; color: #FFFFFF; text-decoration:none; padding:20px; border-radius:5px; margin:0 auto; text-align:center; } .facebooklogin:hover{ background-color:#4158cc; } .user{ font-size:100%; font-family:arial; display:block; width:200px; height:20px; background-color:#142dac; color: #FFFFFF; text-decoration:none; padding:20px; border-radius:5px; margin:0 auto; text-align:center; } </style> </head> <body> <?php /* Author: Belal Khan website: www.simplifiedcoding.net */ //INCLUDING LIBRARIES require_once('lib/Facebook/FacebookSession.php'); require_once('lib/Facebook/FacebookRequest.php'); require_once('lib/Facebook/FacebookResponse.php'); require_once('lib/Facebook/FacebookSDKException.php'); require_once('lib/Facebook/FacebookRequestException.php'); require_once('lib/Facebook/FacebookRedirectLoginHelper.php'); require_once('lib/Facebook/FacebookAuthorizationException.php'); require_once('lib/Facebook/FacebookAuthorizationException.php'); require_once('lib/Facebook/GraphObject.php'); require_once('lib/Facebook/GraphUser.php'); require_once('lib/Facebook/GraphSessionInfo.php'); require_once('lib/Facebook/Entities/AccessToken.php'); require_once('lib/Facebook/HttpClients/FacebookCurl.php'); require_once('lib/Facebook/HttpClients/FacebookHttpable.php'); require_once('lib/Facebook/HttpClients/FacebookCurlHttpClient.php'); //USING NAMESPACES use Facebook\FacebookSession; use Facebook\FacebookRedirectLoginHelper; use Facebook\FacebookRequest; use Facebook\FacebookResponse; use Facebook\FacebookSDKException; use Facebook\FacebookRequestException; use Facebook\FacebookAuthorizationException; use Facebook\GraphObject; use Facebook\GraphUser; use Facebook\GraphSessionInfo; use Facebook\HttpClients\FacebookHttpable; use Facebook\HttpClients\FacebookCurlHttpClient; use Facebook\HttpClients\FacebookCurl; //STARTING SESSION session_start(); FacebookSession::setDefaultApplication(APP_ID,APP_SECRET); $helper = new FacebookRedirectLoginHelper(REDIRECT_URL); $sess = $helper->getSessionFromRedirect(); if(isset($sess)){ $request = new FacebookRequest($sess, 'GET', '/me'); $response = $request->execute(); $graph = $response->getGraphObject(GraphUser::className()); $name = $graph->getName(); ?> <div class='user'> <?php echo "hi $name"; ?> </div> <?php }else{ ?> <a class="facebooklogin" href='<?php echo $helper->getLoginUrl();?>'> <i class="fa fa-facebook"></i> Sign in with Facebook </a> <?php } ?> </body> </html> |
- Try running the project from your local host. Remember you need an active internet connection.
- If you are having some trouble you can download my source code from below.
[easy_media_download url=”https://dl.dropboxusercontent.com/s/wwnjxmzc58jzzgu/php-facebook-login.zip?dl=0″ text=”Download Source”]
- So thats it for this php facebook login tutorial, we will do some more interesting stuffs in upcoming tutorial. 🙂