how to make facebook login php mysql

how to make facebook login php mysql

To implement a basic Facebook login system using PHP and MySQL, you can follow these steps:

  1. Set up a Facebook App:

    • Go to the Facebook Developers website and create a new app.
    • Note down your App ID and App Secret.
  2. Create a MySQL Database:

    • Create a MySQL database to store user information.
  3. Set up your PHP files:

    index.php (This will be your landing page with a Facebook login button):

    php
    <?php session_start(); require_once 'config.php'; ?> <!DOCTYPE html> <html> <head> <title>Facebook Login</title> </head> <body> <?php if (!isset($_SESSION['facebook_access_token'])): ?> <a href="<?php echo $loginUrl; ?>">Login with Facebook</a> <?php else: ?> <a href="logout.php">Logout</a> <?php endif; ?> </body> </html>

    config.php (This file will contain your Facebook App credentials and handle authentication):

    php
    <?php require_once 'Facebook/autoload.php'; $fb = new Facebook\Facebook([ 'app_id' => 'YOUR_APP_ID', 'app_secret' => 'YOUR_APP_SECRET', 'default_graph_version' => 'v2.10', ]); $helper = $fb->getRedirectLoginHelper(); $permissions = ['email']; // optional permissions $loginUrl = $helper->getLoginUrl('http://yourwebsite.com/fb-callback.php', $permissions);

    fb-callback.php (This file will handle the callback from Facebook after authentication):

    php
    <?php session_start(); require_once 'config.php'; try { $accessToken = $helper->getAccessToken(); } catch(Facebook\Exception\ResponseException $e) { echo 'Graph returned an error: ' . $e->getMessage(); exit; } catch(Facebook\Exception\SDKException $e) { echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } if (!isset($accessToken)) { if ($helper->getError()) { header('HTTP/1.0 401 Unauthorized'); echo "Error: " . $helper->getError() . "\n"; echo "Error Code: " . $helper->getErrorCode() . "\n"; echo "Error Reason: " . $helper->getErrorReason() . "\n"; echo "Error Description: " . $helper->getErrorDescription() . "\n"; } else { header('HTTP/1.0 400 Bad Request'); echo 'Bad request'; } exit; } // Logged in $_SESSION['facebook_access_token'] = (string) $accessToken; // Redirect to your home page header('Location: index.php');

    logout.php (This file will handle logout):

    php
    <?php session_start(); unset($_SESSION['facebook_access_token']); session_destroy(); header('Location: index.php');
  4. Handling MySQL:

    • When a user logs in for the first time using Facebook, save their information (like email) to your MySQL database.
    • When a user logs in subsequently, retrieve their information from the database.

Remember to replace 'YOUR_APP_ID' and 'YOUR_APP_SECRET' with your actual Facebook App credentials.

This is a basic implementation. Depending on your requirements, you may need to extend it further. Also, ensure you follow Facebook's guidelines and best practices for handling user data and authentication.

Previous Post Next Post