Login with facebook : You can use facebook login in your websites to allow users to login using their facebook account.you don’t need an extra registration and user management for your sites.you can also manage users in your facebook application page. This article explains how to integrate “Facebook login” to your websites using Facebook PHP SDK with an example and demo.
» src
- – -base_facebook.php
- – -facebook.php
» fbconfig.php
» index.php
» logout.php
Type your app name and press continue.
Step 2 » After completing captcha verification . you can see your App ID and App secret .
Step 3 » Now complete the other basic details .
» Find App Domains: under Basic Info and Enter your domain details like demos.zombi.com.
» Find Site URL: under Website with Facebook Login and type your site url like http://demos.zombi.com/.
Step 5 » Now open fbconfig.php file and enter your app ID and secrets .
Step 6 » And change logout URL path ( Enter Full path ) .
Logout.php file is used only to destroy facebook session and return back to your home page .
Step 9 » If you want you can enter your home page in the code.
Step 10 » You can change this file as per your need . Split this file into 2 parts before login and after login.
Login with facebook
This article contains 3 main files and 2 facebook SDK files in src folder.» src
- – -base_facebook.php
- – -facebook.php
» fbconfig.php
» index.php
» logout.php
Creating facebook App for your site
Step 1 » Goto https://developers.facebook.com/apps/ and Create a new app .Type your app name and press continue.
Step 2 » After completing captcha verification . you can see your App ID and App secret .
Step 3 » Now complete the other basic details .
» Find App Domains: under Basic Info and Enter your domain details like demos.zombi.com.
» Find Site URL: under Website with Facebook Login and type your site url like http://demos.zombi.com/.
fbconfig.php file overview
Step 4 » Download the Demo package here Login with facebook .Step 5 » Now open fbconfig.php file and enter your app ID and secrets .
$facebook = new Facebook(array(
'appId' => '642945345353456456', // Facebook App ID
'secret' => '856379884d66aeefdfgdgdgtesdgdrgr', // Facebook App Secret
'cookie' => true,
));
Step 6 » And change logout URL path ( Enter Full path ) .
$logoutUrl = $facebook->getLogoutUrl(array(
'next' => 'http://demos.krizna.com/logout.php', // Logout URL full path
));
Step 7 » Enter the permission details, Here i need permission to view the user email registered with their facebook account. Here is the list of permissionshttps://developers.facebook.com/docs/reference/login/#permissions and scope names you can call in this sections.
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email', // Permissions to request from the user
));
Step 8 » Finally full code of fbconfig.php file. See the commented lines for more details
<?php
require 'src/facebook.php'; // Include facebook SDK file
$facebook = new Facebook(array(
'appId' => '642945345353456456', // Facebook App ID
'secret' => '856379884d66aeefdfgdgdgtesdgdrgr', // Facebook App Secret
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
$fbid = $user_profile['id']; // To Get Facebook ID
$fbuname = $user_profile['username']; // To Get Facebook Username
$fbfullname = $user_profile['name']; // To Get Facebook full name
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl(array(
'next' => 'http://demos.krizna.com/1353/logout.php', // Logout URL full path
));
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email', // Permissions to request from the user
));
}
?>
logout.php file overview
Logout.php file is used only to destroy facebook session and return back to your home page .
Step 9 » If you want you can enter your home page in the code.
<?php
require 'fbconfig.php'; // Include fbconfig.php file
$facebook->destroySession(); // to destroy facebook sesssion
header("Location: " ."./"); // you can enter home page here ( Eg : header("Location: " ."http://demo.krizna.com");
?>
index.php file overview
Step 10 » You can change this file as per your need . Split this file into 2 parts before login and after login.
<?php
require 'fbconfig.php'; // Include fbconfig.php file
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>Login with facebook</title>
--- --- ---
css stuff
--- --- ----
</head>
<body>
<?php if ($user): ?>
-- --- - - - -- -
Display content After user login
-- -- - --- ---- -- -
<?php else: ?>
-- --- - - - -- -
Display content before login
-- -- - --- ---- -- -
<?php endif ?>
</body>
</html>
Finally full code of index.php file .
<?php
require 'fbconfig.php'; // Include fbconfig.php file
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>Login with Facebook</title>
<link href="http://www.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" rel="stylesheet">
</head>
<body>
<?php if ($user): ?> <!-- After user login -->
<div class="container">
<div class="hero-unit">
<h1>Hello <?php echo $fbuname; ?></h1>
<p>Welcome to "facebook login" tutorial</p>
</div>
<div class="span4">
<ul class="nav nav-list">
<li class="nav-header">Image</li>
<li><img src="https://graph.facebook.com/<?php echo $user; ?>/picture"></li>
<li class="nav-header">Facebook ID</li>
<li><?php echo $fbid; ?></li>
<li class="nav-header">Facebook Username</li>
<li><?php echo $fbuname; ?></li>
<li class="nav-header">Facebook fullname</li>
<li><?php echo $fbfullname; ?></li>
<div><a href="<?php echo $logoutUrl; ?>">Logout</a></div>
</ul></div></div>
<?php else: ?> <!-- Before login -->
<div class="container">
<h1>>Login with Facebook</h1>
Not Connected
<div>
<a href="<?php echo $loginUrl; ?>">Login with Facebook</a></div>
</div>
<?php endif ?>
</body>
</html>
No comments:
Post a Comment