→ Are you a new visitor? Please visit the page guidance for new visitors ←

How to login using Facebook php sdk

In this article i’m going to go in a little detail and show you how to create a very simple Facebook login script that you can then use in your own website. Its pretty straight forward really. This can be developed further of course and make more with your users, like signup, conditioning based on gender and many more.

Download Facebook PHP SDK

First thing you need to go to facebook here and download the php Facebook sdk.

https://developers.facebook.com/docs/php/gettingstarted

You should have there a link “Download the PHP SDK”. Click on this and you should be redirected to GIT. Here click to “Download ZIP” file.

You only really need the files from the folder “src“. We will keep that folder for the rest of this article.

Create Facebook APP

Next, you need to create your own app in your Facebook account. On the same page on 

https://developers.facebook.com

Go to Apps and click to Create a new App. Follow the steps and you should be alright.

Now that you created the Facebook app, you should have something similar to bellow:

Facebook App Settings

Facebook App Settings

In section App Domains, add your own domain from where you are connection. Add a new platform and choose website, there add your own website url from where you testing / connecting with facebook. I used a subdomain which is pointing to my local PC here.

Login script using Facebook PHP SDK

Once this is done, we only need a little script, I’ve written one below for you guys really fast. You can see more documentation here:

https://developers.facebook.com/docs/php/gettingstarted/

In the above code snippet I also commented what you need to know and what you need to change, you can do more than just allowing access to specific section and I did made the example code in such a way.

For example, you have your own Users table and a field called fb_id, each user with their own custom profile pages. What you want is based on their Facebook ID, to match the proper user and show him the correct dashboard page.

For this you need to store your the Facebook ids, probably either have the signup using facebook, or just allow them to login to their account as usual, but have them an option connect to their facebook account and connect their profile to their facebook id, of course, only on the user’s request. Do not force your users!

In the above code snipped I’ve shown you a really quick way of how this is working so that you can use change it based on what you need. I haven’t test it unfortunately, if you find any errors, kindly let me know, I did however checked the code and it should be correct.

What you basically do, is a query and check if the Facebook ID that exists in your database matches the one from your user that logged in, then based on this, allow him access or send him to the registration form.

That’s all for now, sorry again for the tardiness, still working hard on some projects, but will see you again in our next article.

Request an article ←