Login with facebook at codeigniter

I am using this, to login with Facebook , but I am not getting user response here, this is the link

http://demos.idiotminds.com/link.php?link=https://www.box.com/s/108pspt0o0oj0fpr6ghf

I tried this solution also for codeigniter

  protected function getCode() {
    $server_info = array_merge($_GET, $_POST, $_COOKIE);

    if (isset($server_info['code'])) {
        if ($this->state !== null &&
                isset($server_info['state']) &&
                $this->state === $server_info['state']) {

            // CSRF state has done its job, so clear it
            $this->state = null;
            $this->clearPersistentData('state');
            return $server_info['code'];
        } else {
            self::errorLog('CSRF state token does not match one provided.');
            return false;
        }
    }

    return false;
}

      

to login with Facebook, but I don't get a value from that $user = $facebook->getUser();

, it returns 0 even though I am logged into Facebook. I have used so many codes for this but no success, kindly help me. I am very sad

+3


source to share


2 answers


Download PHP SDK for Facebook

Now create a folder in your application \ libarires "facebook"

Place the "SRC" folder in the PHP SDK

Now create a file with FacebookApp.php in this folder and put this 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'); 
    }
  }
}

      



Now in your controller load this library $this->load->library('facebook/FacebookApp)

in method

$obj_fb = new FacebookApp(); $fb_user_data = $obj_fb->get_user(); $data['fb_login_url'] = $obj_fb->login_url();

put fb_login_url in the href of the login button and you will now be logged in.

Hope it helps you.

+3


source


Rahul, I had a similar problem. You can find a pretty good solution here .



If you still can't figure out the solution, why not take a look at the JavaScript SDK . It's pretty straight forward, then use AJAX to react to the response you get from Facebook.

0


source







All Articles