Login With Facebook Using PHP CodeIgniter

Login With Facebook Using PHP CodeIgniter

Feb 10, 2019 / PHP / CodeIgniter /

Facebook login is simply an open authentication that is used for providing access to the server data after authorizing a user via third party. In this tutorial, we are going to explain Login With Facebook Using PHP CodeIgniter. how you can implement Facebook login via open authentication to connect with your website using php CodeIgniter.

You can achieve this login using Facebook SDK in PHP.

First Download the Facebook PHP SDK and save it in your CodeIgniter’s libraries folder.

Step 1: Now generate your Facebook app id and Facebook app secret key from https://developers.facebook.com and sign in by an existing Facebook username or password.

Below is our complete code with short explanation :

Step 2: Go to the config folder (located at the root of the CI installation) and create a new facebook.php” file.

now that App ID and App Secret Key you have to add into your application code as shown below:


$config['facebook_app_id']              = 'YOUR-APP-ID';
$config['facebook_app_secret']          = 'YOUR-APP-SECRET';
$config['facebook_login_type']          = 'web';
$config['facebook_login_redirect_url']  = 'facebook_login';
$config['facebook_logout_redirect_url'] = 'facebook_login/logout';
$config['facebook_permissions']         = array('email');
$config['facebook_graph_version']       = 'v2.6';
$config['facebook_auth_on_load']        = TRUE;

Step 3: Go to the controller folder (located at the root of the CI installation) and create a new facebook_login.php” file.



defined('BASEPATH') OR exit('No direct script access allowed');
session_start();

class facebook_login extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('facebook');
    }

    public function index() {
        $data['fb'] = $this->getauth();
        $data['LogonUrl'] =  $this->facebook->login_url();
        $this->load->view('fb_login', $data);
        
    }

    public function getauth() {
        $userProfile = array();
        if ($this->facebook->is_authenticated()) {
            $userProfile = $this->facebook->request('get', '/me?fields=id,first_name,last_name,email,gender,locale,picture');
       }
        return $userProfile;
    }

    public function logout() {
        $this->facebook->destroy_session();
        redirect('/facebook_login');
    }

Step 4: Go to the view folder (located at the root of the CI installation) and create a new fb_login.php” file.


<html>
<head>
  <title>Login with Facebook</title>
</head>
<body>
<?php if (isset($fb['id']) && !empty($fb['id'])) { ?>
   
        <a href="<?php echo base_url('facebook_login/logout') ?>">Logout from facebook</a>
    
        <?php } else { ?>
    
        <a href="<?php echo $LogonUrl ?>"><img src="<?php echo base_url('assets_signi/flogin.png') ?>"></a>
    

        <?php } ?>

        <?php
        // print_r($fb); 
        if (isset($fb['id']) && !empty($fb['id'])) {
            ?>
   
                <table class="table">
                    <tr>
                        <td>Id: </td>
                        <td><?php echo $fb['id'] ?></td>
                    </tr>
                    <tr>
                        <td>Name: </td>
                        <td><?php echo $fb['first_name'] . ' ' . $fb['last_name'] ?></td>
                    </tr>
                    <tr>
                        <td>Profile Pic</td>
                        <td><img src="<?php echo $fb['picture']['data']['url']; ?>"></td>
                    </tr>
                    <tr>
                        <td>Email: </td>
                        <td><?php echo $fb['email'] ?></td>
                    </tr>
                </table>
           
<?php } ?>
</body>
</html>

Comments

Mohammad ( Feb 10, 2019 06:08:28 )

Thank you..

Would you like to share your thoughts?

Your email address will not be published. Required field are marked*

Copyright 2018. All rights are reserved Significant Techno Web Design & Web Development By Significant Techno