Facebook Connect – Facebook Login System with PHP and MySQL
In this tutorial you will see how to make a Login System with facebook using php and mysql. User will login with their facebook account, give permission to access basic profile, and their name with email will be stored in database if they are not stored before.
Live Demo
Login with Facebook and creating Facebook App for Website.
In case if you are a newbie, Watch the following Video before proceeding.
Steps to Create Facebook Login System with PHP and MySQL
Lets Code Now, Create a Database named “login_system” or Anything and Create a Table “users”
1 2 3 4 5 6 7 8 |
CREATE DATABASE IF NOT EXISTS login_system; USE login_system; CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1; |
I hope you have created a facebook app to get APP_ID and APP_SECRET. Set the config.php with all your site and facebook related credentials
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// database connection define('DB_DRIVER', 'mysql'); define('DB_SERVER', 'localhost'); define('DB_SERVER_USERNAME', 'root'); define('DB_SERVER_PASSWORD', ''); define('DB_DATABASE', 'login_system'); // site URL and facebook credentials define("APP_ID", "your app id"); define("APP_SECRET", "your app secret"); /* make sure the url end with a trailing slash */ define("SITE_URL", "http://demo.phphive.info/login-system-with-facebook/"); /* the page where you will be redirected after login */ define("REDIRECT_URL", SITE_URL."facebook_login.php"); /* Email permission for fetching emails. */ define("PERMISSIONS", "email"); // create a facebook object $facebook = new Facebook(array('appId' => APP_ID, 'secret' => APP_SECRET)); $userID = $facebook->getUser(); // Login or logout url will be needed depending on current user login state. if ($userID) { $logoutURL = $facebook->getLogoutUrl(array('next' => SITE_URL . 'logout.php')); } else { $loginURL = $facebook->getLoginUrl(array('scope' => PERMISSIONS, 'redirect_uri' => REDIRECT_URL)); } |
Set your index.php with the login URL in the anchor tag. So when the user clicks on it it will be redirected to facebook site.
1 2 3 4 5 |
<!-- Add login URL in index.php --> <a class="btn btn-block btn-social btn-facebook" href="<?php echo $loginURL; ?>">Login with Facebook</a> <!-- Add logout URL in home.php --> <a class="btn btn-block btn-social btn-facebook" href="<?php echo $logoutURL; ?>">Logout</a> |
User will login with their facebook accounts and give permission to the application. If user dont give permission then user will be redirected back to index page. If the user gives permission to the app then user information will be returned and data will be processed accordingly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
require_once './config.php'; if ($userID) { try { if ($_SESSION["user_id"] == "") { // fetch user details. $user_profile = $facebook->api('/me'); // Now check if user exist with same email ID $sql = "SELECT COUNT(*) AS count from users where email = :email_id"; try { $stmt = $DB->prepare($sql); $stmt->bindValue(":email_id", $user_profile["email"]); $stmt->execute(); $result = $stmt->fetchAll(); if ($result[0]["count"] > 0) { // User Exist $_SESSION["name"] = $user_profile["name"]; $_SESSION["email"] = $user_profile["email"]; $_SESSION["new_user"] = "no"; } else { // New user, Insert in database $sql = "INSERT INTO `users` (`name`, `email`) VALUES " . "( :name, :email)"; $stmt = $DB->prepare($sql); $stmt->bindValue(":name", $user_profile["name"]); $stmt->bindValue(":email", $user_profile["email"]); $stmt->execute(); $result = $stmt->rowCount(); if ($result > 0) { $_SESSION["name"] = $user_profile["name"]; $_SESSION["email"] = $user_profile["email"]; $_SESSION["new_user"] = "yes"; } } } catch (Exception $ex) { #echo $ex->getMessage(); } header("location:home.php"); } $_SESSION["user_id"] = $userID; } catch (FacebookApiException $e) { $userID = NULL; } } else { // if facebook user not authentic header("location:index.php"); } |
Just the final step to display user the message based on database transaction i.e. if user has registered for the first time or logged in back with their facebook account.
1 2 3 4 5 6 |
<?php if ($_SESSION["new_user"] == "yes") { ?> <h2>Thank you <?php echo $_SESSION["name"] ?>, for registering with us!!!</h2> <?php } else { ?> <h2>Welcome back <?php echo $_SESSION["name"] ?>!!!</h2> <?php } ?> <h5>Your email id is: <span style="text-decoration:underline;"><?php echo $_SESSION["email"]; ?></span></h5> |
I got a fatal error : undefined function call to curl(); how to solve that
Contact Your Web Hosting Company for Enabling Curl !
OR
If you are using Xampp then Enable it by uncommenting Curl Extension from php.ini
I followed all instruction , use your code
but I’m not getting email address
I tried print_r in facebook.login.php such like
require_once ‘./config.php’;
// Only if user is logged in and given permission, we can fetch user details
if ($userID) {
try {
if ($_SESSION[“user_id”] == “”) {
// fetch user details.
$user_profile = $facebook->api(‘/me’);
print_r( $user_profile );
it shows array of 2 elements
1) username
2)id
why I’m not getting email address??
but in your demo , it shows email
here is the permission code
define(“PERMISSIONS”, “email,public_profile,user_friends”);
plz help