Skip to main content

Facebook Login In PHP

As we all know Time is Money. When there's a long form on your site for registration you could sometimes lose your visitors. To make this registration step a success, adding a small social login button to your site may cut down lot of burden on visitors side and will help you in getting more attention on your data rather than wasting time on the lengthy registration process. By doing so visitor will be glad to see that you care for their precious time.
 Facebook login in PHP

What do you think will there be anyone in this small world who is not having a Facebook account? According to recent stats 1 in every 13 person in this world is using facebook.

To get a working login button on your site you'll have to know the facebook API in depth but I'll try to make it as brief as possible so that I can prove I also care for you guys. 
(Note: To get this facebook login your server should have CURL enabled)

Get a Facebook developer account 

Creating a developer account on Facebook is not difficult. Just visit https://developers.facebook.com/ and register yourself as a developer. You will get a working developer account for Facebook in no time. Congratulations, you're a Facebook developer now. 

Create an app for your website 

First you need to have a working website on which you will deploy the registration or login button. You will need the domain for configuring your app. Visit https://developers.facebook.com/ << Apps << Create a new app. There will be a form asking for name, namespace and category. Fill the form appropriately. Once you pass the security check by entering right captcha code App ID and App secret will be shown. Copy those codes separately onto notepad or just keep that tab open.

Configure your app

Click on Settings tab on left side of webpage and enter the domain where login and register button are to be used. Once you enter your domain click on ADD PLATFORM just below the form you filled and select the platform where login button is going to be used. As we are going to add Login button for a website, select Website. Enter site URL in both the input boxes and hit Save Changes. All the changes to be done on facebook platform is complete now. For your reference I added a screenshot of my demo app created. (Note: If you are using this login on your localserver enter the domain as http://localhost)




Server side deployment

Download some required files from Drive . These files are openly available on facebook for PHP developers and free to use. You can visit https://developers.facebook.com/docs/reference/php/ to gather more details on Facebook SDK for PHP. Extract downloaded files and open index.php in notepad or any other text editor. Replace the code shown in red color below with your app settings. These code is also available on facebook 

<?php // Remember to copy files from the SDK's src/ directory to a // directory in your application on the server, such as php-sdk/ require_once('php-sdk/facebook.php'); $config = array( 'appId' => 'YOUR_APP_ID', 'secret' => 'YOUR_SECRET_KEY', 'allowSignedRequest' => false // optional but should be set to false for non-canvas apps ); $facebook = new Facebook($config); $user_id = $facebook->getUser(); ?> <html> <head></head> <body> <?php if($user_id) { // We have a user ID, so probably a logged in user. // If not, we'll get an exception, which we handle below. try { $user_profile = $facebook->api('/me','GET'); echo "Name: " . $user_profile['name']; } catch(FacebookApiException $e) { // If the user is logged out, you can have a // user ID even though the access token is invalid. // In this case, we'll get an exception, so we'll // just ask the user to login again here. $login_url = $facebook->getLoginUrl(); echo 'Please <a href="' . $login_url . '">login.</a>'; error_log($e->getType()); error_log($e->getMessage()); } } else { // No user, print a link for the user to login $login_url = $facebook->getLoginUrl(); echo 'Please <a href="' . $login_url . '">login.</a>'; } ?> </body> </html>

Thats it, open this file from your server and you'll get a login link. Add this link on your site and style it as per your requirement. I already mentioned above as a note and I am still making you note that it will not work on server which is not cURL enabled.

If you have any questions, please feel free to ask. I would be happy to answer them.

Comments

  1. Thank you sir ji..............
    keep it up...........................................

    ReplyDelete

Post a Comment

Popular posts from this blog

USB Boot Procedure

Simple step-by-step solution to create a bootable usb in cmd: 1)   Take a pen drive of more than 4gb . 2)   Now the most important step is to open cmd from start->search program and files->type cmd . 3)   Now in the new window, type diskpart and hit enter. 4)   Type the command list disk . Now, note the disk which have size identical to that of your pen drive. eg. disk 1. 5)   Type the command select disk 1 . 6)   Then type clean and hit enter. 7)   Just type create partition primary and press enter. 8)  Then run the command list partition . 9)  Type select partition 1 and hit enter. 10) Format it using the command format fs=ntfs . 11) Type the commands active , assign and exit in sequence. Now just copy all the files from Boot-able disc of  windows 7 to the pen drive and your pen drive is ready to be used as a boot-able device.