Facebook Connect & CodeIgniter

So lately, I have been trying to incorporate Facebook Connect with a CodeIgniter-based web application. After much searching, I was able to incorporate different tutorials/tips to get it working. This is a pretty short tutorial, but I hope it clears some questions about CodeIgniter and FB Connect.

Step 1: Download Facebook’s PHP Client Library

The first step is to download the PHP Client Library from Facebook, and put it into your application/libraries/ folder. I prefer to put it into a sub-folder inside Libraries, but that’s your choice.

Step 2: Download Elliot Haughlin’s Facebook Connect CodeIgniter Library

Next, download Elliot’s Facebook Connect CodeIgniter Library. This will really reduce your workload when it comes to building PHP-based Facebook apps. Put the downloaded files into the appropriate folders. The most important file here is the facebook-connect.php file, which should go into your application/libraries folder. There is also a sample controller and sample view to help you out. Fool around with it for a bit!

Step 3: Fixing the Invalid Session Error

Okay, so after playing around with Elliot’s library, you may have noticed a session error being shown saying:

Session key invalid or no longer valid

If you aren’t getting this error, here is a way to replicate the situation.

  1. Use Facebook Connect to log into your application. This will automatically log you into Facebook as well.
  2. Log out of Facebook, while still being logged into your application.
  3. Refresh your application page (your view). You should now get the error.

Fixing this is actually quite straightforward, but is not really documented well. Find the following line of code in the facebook-connect.php file:

$this->user_id = $this->fb->get_loggedin_user();

Replace it with the following snippet:

if($this->fb->get_loggedin_user()) {
try {
//Get some sample data from facebook, you can do whatever you want here really..
$this->user_id = $this->client->fql_query('SELECT uid, pic_square, first_name FROM user WHERE uid = ' . $this->fb->get_loggedin_user());
} catch (Exception $ex) {
//Need to destroy the session. This is the important part!

Step 4: Creating a successful Logout

Another problem I found with the Facebook Codeigniter library is that upon logout, your user details are not removed. Even if you call clear_cookie_state(), one cookie still persists. This cookie is called fbsetting_[api_key]fb, where [api_key] is your api key. You can make a complete logout by doing the following in your controller:

function logout() {


	catch (FacebookRestClientException $e) {
		//you'll want to catch this
		//it fails all the time
	//Load your view as needed

You can call this method from your view as such:

	<!--Logout Button-->
<a href="javascript:logout()">
	<img id="fb_logout_image" src="http://static.ak.fbcdn.net/images/fbconnect/logout-buttons/logout_small.gif" alt="Connect"/>
<script type="text/javascript">
	function logout(){
    1. That’s weird. I’ll look into it. In either case, this example is pretty outdated – I’ll try to put up an updated one which uses Facebook’s new graph API. Unfortunately, I’m not doing any CodeIgniter work at the moment.

Comments are closed.