skip to Main Content

I want to create a login with Facebook in my website. I found a code in the internet that made it simple loading the library of Facebook php sdk. I tried the code but it doesn’t work in me. Please help me how to do login with facebook in codeigniter.

Here is the code :

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

require_once( APPPATH . 'libraries/facebook/src/facebook.php' );

class FacebookApp extends Facebook {

var $ci;

var $facebook;

var $scope;

public function __construct() {
  $this->ci =& get_instance();

  $this->facebook = new Facebook(array('appId'  => $this->ci->config->item('app_id'),'secret' => $this->ci->config->item('app_secret'), 'cookie' => true));

$this->scope = 'public_profile';
}

public function login_url() {
  $params = array('scope' => $this->scope);
  return $this->facebook->getLoginUrl($params);
}

public function logout_url() {
  return $this->facebook->getLogoutUrl(array('next' => base_url() .'logout'));
}

public function getFbObj(){
  return $this->facebook;
}

public function get_user() {
  $data = array();
  $data['fb_user'] = $this->facebook->getUser();
  if ($data['fb_user']) {
    try {
      $data['fb_user_profile'] = $this->facebook->api('/me');
      return $data;
    } catch (FacebookApiException $e) {
      $this->facebook->destroySession();
      $fb_login_url = $this->facebook->getLoginUrl(array('scope' => $this->scope));
      redirect($fb_login_url, 'refresh'); 
    }
  }
}

here is my controller :

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class User_Authentication extends CI_Controller
{
    function __construct() {
        parent::__construct();
        // Load user model
        $this->load->model('auth/user_model');
        $this->load->library('facebook/FacebookApp');
    }

    public function index(){
        $obj_fb = new FacebookApp();
        $fb_user_data = $obj_fb->get_user();
        $data['fb_login_url'] = $obj_fb->login_url();
    }
}

and here is my view:

<div class="modal fade" id="choose" role="dialog">
  <div class="modal-dialog"> 
    <div class="modal-content">
      <div class="modal-header  btn-success">
               <h3 class="modal-title">Connect with</h3>
      </div><!-- modal-header -->
        <div class="modal-body">
          <div class="connectwith">
          <form class="form-horizontal" id="payment">
              <button onclick="<?php echo base_url()?>User_authentication" class="btn btn-primary"> Continue with Facebook </button>
          </form><!-- form-horizontal -->
          </div>
        </div><!-- modal-body -->
    </div><!-- modal-content -->
  </div><!-- modal-dialog -->
</div><!-- choose -->

it shows no error when i check it on my console, i don’t know what’s happen, i am a beginner in adding libraries. Please help me with this. Thanks

2

Answers


  1. First of all load url helper so you can use base_url().Load helper in controller…

    function __construct() {
            parent::__construct();
           //Load Helper
             $this->load->helper('url');
            // Load user model
            $this->load->model('auth/user_model');
            $this->load->library('facebook/FacebookApp');
        }
    

    In your view replace

    onclick="<?php echo base_url()?>User_authentication"
    

    To

    onclick="<?php echo base_url('user_authentication');?>"
    
    Login or Signup to reply.
  2. Use this github https://github.com/bhawnam193/php-programs/tree/master/facebook-login for using fb login .
    Firstly make a facebook app and replace the

    $app_id ,$app_secret, $site_url 
    

    in file fbaccess.php.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search