CodeIgniter Tutorial: How To Login With Facebook Using CodeIgniter

This video learn how to login with Facebook using CodeIgniter.



Facebook.php (Save to application/config folder)

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

$config['appId']   = 'Your App ID';
$config['secret']  = 'Your App Secret';


?>

Welcome.php (Save to application/controllers folder)

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

public function __construct(){
parent::__construct();

        // To use site_url and redirect on this controller.
        $this->load->helper('url');
}

public function login(){

$this->load->library('facebook'); // Automatically picks appId and secret from config
        // OR
        // You can pass different one like this
        //$this->load->library('facebook', array(
        //    'appId' => 'APP_ID',
        //    'secret' => 'SECRET',
        //    ));

$user = $this->facebook->getUser();
        
        if ($user) {
            try {
                $data['user_profile'] = $this->facebook->api('/me');
            } catch (FacebookApiException $e) {
                $user = null;
            }
        }else {
            $this->facebook->destroySession();
        }

        if ($user) {

            $data['logout_url'] = site_url('welcome/logout'); // Logs off application
            // OR 
            // Logs off FB!
            // $data['logout_url'] = $this->facebook->getLogoutUrl();

        } else {
            $data['login_url'] = $this->facebook->getLoginUrl(array(
                'redirect_uri' => site_url('welcome/login'), 
                'scope' => array("email") // permissions here
            ));
        }
        $this->load->view('login',$data);

}

    public function logout(){

        $this->load->library('facebook');

        // Logs off session from website
        $this->facebook->destroySession();
        // Make sure you destory website session as well.

        redirect('welcome/login');
    }

}

login.php (Save to application/views folder)


<html>

<head>
<title>Login with Facebook Using CodeIgniter</title>
</head>

<body>

<div class="container">
<form>
<!-- @user_profile check when user login successed  -->
<?php if (@$user_profile):  // call var_dump($user_profile) to view all data ?>
<!-- Display profile photo -->
<img src="https://graph.facebook.com/<?=$user_profile['id']?>/picture?type=large" style="width: 140px; height: 140px;">
<!-- Display profile name -->
<h2><?=$user_profile['name']?></h2>
<!-- Create link to facebook profile -->
<a href="<?=$user_profile['link']?>">View Profile</a>
<!-- Create link logout -->
<a href="<?= $logout_url ?>">Logout</a> 
   <!-- Display login when start home page first -->
<?php else: ?>
<h2>Login with Facebook Using CodeIgniter</h2>
<a href="<?= $login_url ?>">Login</a> 
<?php endif; ?>
</form>
</div>

</body>

</html>

libraries download from demo and then extract to application/libraries folder

Demo
http://downloads.ziddu.com/download/24375128/cfblogin.zip.html

Read more blog

Share this

Related Posts

Previous
Next Post »

2 comments

comments
January 30, 2016 at 11:21 PM delete

A PHP Error was encountered

Severity: Notice

Message: Undefined index: link

Filename: views/login.php

Line Number: 18
">View Profile Logout

Reply
avatar
Anonymous
February 8, 2016 at 3:40 AM delete

Warning: include(/home/hasnat/www/softograph_/codeigniterProjects_/new-misir.dev/public_frontend/app/views/errors/html/error_php.php): failed to open stream: No such file or directory in /home/hasnat/www/softograph_/codeigniterProjects_/new-misir.dev/public_frontend/ci_3.0.0/core/Exceptions.php on line 243

Warning: include(): Failed opening '/home/hasnat/www/softograph_/codeigniterProjects_/new-misir.dev/public_frontend/app/views/errors/html/error_php.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/hasnat/www/softograph_/codeigniterProjects_/new-misir.dev/public_frontend/ci_3.0.0/core/Exceptions.php on line 243

Warning: include(/home/hasnat/www/softograph_/codeigniterProjects_/new-misir.dev/public_frontend/app/views/errors/html/error_php.php): failed to open stream: No such file or directory in /home/hasnat/www/softograph_/codeigniterProjects_/new-misir.dev/public_frontend/ci_3.0.0/core/Exceptions.php on line 243

Warning: include(): Failed opening '/home/hasnat/www/softograph_/codeigniterProjects_/new-misir.dev/public_frontend/app/views/errors/html/error_php.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/hasnat/www/softograph_/codeigniterProjects_/new-misir.dev/public_frontend/ci_3.0.0/core/Exceptions.php on line 243

Reply
avatar