Note: This post is very old, please use Facebook SDK4 at https://github.com/facebook/facebook-php-sdk-v4 for implementing Connect with Facebook or for using Facebook graph API in your websites.
Follow the below simple steps and implement Connect with Facebook in your website.
$config['appId'] = 'Your Facebook App ID'; $config['secret'] = 'Your secret Code';
<?php
include(APPPATH.'libraries/facebook/facebook.php');
class Fb_connect extends Facebook
{
//declare public variables
public $user = NULL;
public $user_id = FALSE;
public $fb = FALSE;
public $fbSession = FALSE;
public $appkey = 0;
//constructor method.
public function __construct() {
$CI = & get_instance();
$CI->config->load("facebook",TRUE);
$config = $CI->config->item('facebook');
parent::__construct($config);
$this->user_id = $this->getUser(); // New code
$me = null;
if ($this->user_id)
{
try
{
$me = $this->api('/me');
$this->user = $me;
}
catch (FacebookApiException $e)
{
error_log($e);
}
}
}
} // end class
Now create a Controller and name it as user, and write the following methods in it.
function loginByFacebook()
{
$this->load->library('fb_connect');
$param['redirect_uri']=site_url("user/facebook");
redirect($this->fb_connect->getLoginUrl($param));
}
function facebook()
{
$this->load->library('fb_connect');
if (!$this->fb_connect->user_id)
{
//Handle not logged in,
}
else
{
$fb_uid = $this->fb_connect->user_id;
$fb_usr = $this->fb_connect->user;
//Hanlde user logged in, you can update your session with the available data
//print_r($fb_usr) will help to see what is returned
}
}
You are done with coding now. Now when ever user need to login with facebook just redirect them to user/loginByFacebook and the login will handled automatically by Facebook. But you need to update the facebook() method with the actions success and failure conditions.
Do you know that you can generate a complete PHP websites in minutes with PHP Website Generator?