– See more at: http:https://thinhcoi.com/wp-content/uploadshttps://thinhcoi.com/wp-content/uploadsaniri.rohttps://thinhcoi.com/wp-content/uploadsgeekhttps://thinhcoi.com/wp-content/uploadstutorialshttps://thinhcoi.com/wp-content/uploadshow-to-make-website-registration-easier-using-facebook-accountshttps://thinhcoi.com/wp-content/uploads#sthash.zKgOnpD4.dpuf
What are we going to build?
In this tutorial we will learn how we can facilitate user registration but letting users register to our site using their facebook accounts. This way, they won’t have to fill in any data, just grant the app access to their facebook account and that’s it! Their new account is created and they can easily login to our website.
In this tutorial we will learn how we can facilitate user registration but letting users register to our site using their facebook accounts. This way, they won’t have to fill in any data, just grant the app access to their facebook account and that’s it! Their new account is created and they can easily login to our website.
We will be making calls to facebooks Graph API in order to retrieve the necessary information. In case you are not familiar with it, you might want to take a look at the documentation to get an idea of what it does.
You can view the working demo of what we are going to build here.
The tutorial will have two parts: the first will cover the basic signuphttps://thinhcoi.com/wp-content/uploadslogin issues, the second will cover the extra features we will add to our app (viewing friends info and posting on the wall).
Let’s get started!
Getting started
You would be using this is you have a website that users can register to and you want to make the registration easier for them by allowing them to register with their facebook accounts. In the tutorial, I will only show you how to use facebook accounts for registration, not how to add this to your existing registration. But it shouldn’t be difficult to integrate if you understand the tutorial. If you have questions on that, just ask.
For this tutorial you will need some basic PHP and Javascript knowledge.
Creating a new facebook app
The first thing we have to do is create a new facebook app. To do this, go here and chose to create a new app. You will get to the screen when you have to fill in the app info. The screen will look something like this:
As you can see in the screenshot, you will have to fill in the following:
- A display name for your app
- Your email address
- The domain where the app will be hosted
- The site url – this is the most important value, it is the url of the page where the app will be redirected after facebook authorization. This page will hold the logic for registering new users and loggin in returning users.
So, make sure you fill in the site url correctly and keep in mind the values at the top of the page (app is and app secret) as we will need them later.
And that’s it! You now have a facebook app. Let’s see how we can use it!
Creating the db
Next we have to create the db table to hold the data about our users. Let’s suppose we want to store the following data about our users:
- Username
- Email
- Name
You will probably want to store more data in a real application, but these will do for the tutorial.
Apart from these, we will also want to store the facebook id of the users. So our database will look something like this:
The php code for connecting to the database looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$server = "YOUR_SERVER_ADDRESS";
$username = "YOUR_USERNAME";
$password = "YOUR_PASSWORD";
$database = "YOUR_DATABASE_NAME";
$connId = mysql_connect($server,$username,$password) or die("Cannot connect to server");
$selectDb = mysql_select_db($database,$connId) or die("Cannot connect to database");
?>
Designing the site
Our demo website will have a few pages:
- index.php – the starting point -the users can choose to signuphttps://thinhcoi.com/wp-content/uploadslogin with facebook from here
- welcome.php – the users will be redirected here after creating a new account
- home.php – the main user page, the user will view some info about their facebook friends, have the options to post a new status on facebook and delete their account
I will leave out any design issues. I have chosen to use the twitter bootstrap library to make a nice demo, but you can choose whatever you like for yours. The design on the website is not in the scope of this tutorial.
Signup with facebook
The main page, index.php will hold one button to allow users to signuphttps://thinhcoi.com/wp-content/uploadslogin. The code looks like this:
So, make sure you fill in the site url correctly and keep in mind the values at the top of the page (app is and app secret) as we will need them later.
And that’s it! You now have a facebook app. Let’s see how we can use it!
Creating the db
Next we have to create the db table to hold the data about our users. Let’s suppose we want to store the following data about our users:
- Username
- Email
- Name
You will probably want to store more data in a real application, but these will do for the tutorial.
Apart from these, we will also want to store the facebook id of the users. So our database will look something like this:
The php code for connecting to the database looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$server = "YOUR_SERVER_ADDRESS";
$username = "YOUR_USERNAME";
$password = "YOUR_PASSWORD";
$database = "YOUR_DATABASE_NAME";
$connId = mysql_connect($server,$username,$password) or die("Cannot connect to server");
$selectDb = mysql_select_db($database,$connId) or die("Cannot connect to database");
?>
Designing the site
Our demo website will have a few pages:
- index.php – the starting point -the users can choose to signuphttps://thinhcoi.com/wp-content/uploadslogin with facebook from here
- welcome.php – the users will be redirected here after creating a new account
- home.php – the main user page, the user will view some info about their facebook friends, have the options to post a new status on facebook and delete their account
I will leave out any design issues. I have chosen to use the twitter bootstrap library to make a nice demo, but you can choose whatever you like for yours. The design on the website is not in the scope of this tutorial.
Signup with facebook
The main page, index.php will hold one button to allow users to signuphttps://thinhcoi.com/wp-content/uploadslogin. The code looks like this:
You will probably want to store more data in a real application, but these will do for the tutorial.
Apart from these, we will also want to store the facebook id of the users. So our database will look something like this:
The php code for connecting to the database looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| <?php
|