Nowadays
users are not interested in filling a big form to registration. Short
registration process helps to get more subscribers for your website.
Login with Facebook is a quick and powerful way to integrate
registration and login system on the website. Facebook is a most
popular social network and most of the users have a Facebook account.
Facebook Login allow users to sign into your website using their
Facebook account credentials without sign up on your website.
This
tutorial will explain how you can implement user login and
registration system with Facebook using PHP and store the user
profile data into the MySQL database. We’ll use Facebook PHP SDK
v5.0 with Facebook Graph API to build Facebook Login system
with PHP and MySQL.
Facebook Apps Creation
To
access Facebook API you need to create Facebook App and specify App
ID & App Secret at the time of call Facebook API. For this Facebook uses OAUTH AUTHENTICATION . Follow the
step-by-step guide to creating and configure a Facebook App from the
App Dashboard.
-
Go to the Facebook App Dashboard and log in with your Facebook account.
-
Create a new Facebook apps with your desired name (like WebLogin).
-
If you want to test Facebook login at the localhost server, then your App Domains should be localhost. Also, localhost domain will only work, once you add platform. For add a platform click on Settings link from the left side menu panel » Click on the Add Platform button » Choose Website category » Enter site URL (http://localhost/facebook_login_with_php/).
-
Once you completed the above steps, your apps settings page would something like the below.
-
Now click on Status & Review link from the left side menu panel and make your apps live. Contact email is required for enable the apps live option. If you have not added apps contact email earlier, go to the settings page and add email. Once you submit the contact email, you would be able to enable the apps live option.
-
Congratulation! your apps creation has completed.
Now
download Facbook SDK for PHP and place it in your project or in
vendor(if you are working in any php framework)
Database Table Creation
To
store the user information from the Facebook database, a table
(users) need to be created in MySQL database. At first, create a
database (like technobrigade)
and run the below SQL on the database. The following SQL creates a
users
table
with some basic fields in the database to hold the Facebook profile
information.
CREATE
TABLE `users`
(
`id`
int(11)
NOT
NULL
AUTO_INCREMENT,
`oauth_provider`
enum('','facebook','google','twitter')
COLLATE utf8_unicode_ci NOT
NULL,
`oauth_uid`
varchar(100)
COLLATE utf8_unicode_ci NOT
NULL,
`first_name`
varchar(50)
COLLATE utf8_unicode_ci NOT
NULL,
`last_name`
varchar(50)
COLLATE utf8_unicode_ci NOT
NULL,
`email`
varchar(100)
COLLATE utf8_unicode_ci NOT
NULL,
`gender`
varchar(10)
COLLATE utf8_unicode_ci NOT
NULL,
`locale`
varchar(10)
COLLATE utf8_unicode_ci NOT
NULL,
`picture`
varchar(255)
COLLATE utf8_unicode_ci NOT
NULL,
`link`
varchar(255)
COLLATE utf8_unicode_ci NOT
NULL,
`created`
datetime NOT
NULL,
`modified`
datetime NOT
NULL,
PRIMARY
KEY
(`id`)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci;
Now, The Coding Process
now
place the below code in your login.php file, and
if working in any php framework put this code in any function in your
login controller and call this when user click on facebook login
button of your project
require_once(ROOT . ‘path/to/facebookSDK’. 'Facebook' . DS . 'autoload.php' );
$appId
= Configure::read('Facebook.appId');
$app_secret
= Configure::read('Facebook.secret');
$facebook
= new \Facebook\Facebook([
'app_id'
=> FACEBOOK_APPID,
'app_secret'
=> FACEBOOK_SECRETKEY,
'default_graph_version'
=> 'v2.8',
'default_access_token'
=> '1895502187345773|91abb134a4db3e05a0fb6e9a11fc8fb6'
]);
$helper
= $facebook->getRedirectLoginHelper();
$permissions
= ['email', 'user_likes', 'user_photos', 'user_hometown',
'user_work_history', 'user_location', 'user_birthday',
'public_profile', 'read_insights']; // optional
$loginUrl
= $helper->getLoginUrl(FACEBOOK_REDIRECT_URICONNECT,
$permissions);
$this->redirect($loginUrl);
after this as you have set the redirect url in facebook app dashboard
if
login is successfull then facebook will redirect you to your redirect
url there you have to place this code
require_once(ROOT . ‘path/to/facebookSDK’. 'Facebook' . DS . 'autoload.php' );
$fb
= new \Facebook\Facebook([
'app_id'
=> FACEBOOK_APPID,
'app_secret'
=> FACEBOOK_SECRETKEY,
'default_graph_version'
=> 'v2.8',
]);
$code
= $_GET['code'];
$fb_token_url
= 'https://graph.facebook.com/v2.8/oauth/access_token?client_id=' .
FACEBOOK_APPID . '&redirect_uri=' . FACEBOOK_REDIRECT_URICONNECT
. '&client_secret=' . FACEBOOK_SECRETKEY . '&code=' . $code;
$forAccessToken
= file_get_contents($fb_token_url);
$accesstoken_decode
= (json_decode($forAccessToken));
$accessToken
= $accesstoken_decode->access_token;
if
(isset($accessToken)) {
$_SESSION['facebook_access_token']
= (string) $accessToken;
$response
= $fb->get('/me?fields=id,name,email', $accessToken);
$user
= $response->getGraphUser();
$facebook_id
= $user['id'];
$fbName
= explode(" ", $user['name']);
$data['first_name']
= $fbName[0];
$data['last_name']
= $fbName[1];
$data['mobile']
= "";
$data['username']
= $user['email'];
$data['loginType']
= 'facebook';
$data['facebookId']
= $facebook_id;
addUser($data);
}
in this above code you will get user name image and facebook id which you can insert in your database by calling any addUser function or updateUser if user is already present
No comments:
Post a Comment