GUIDE: Facebook Login with CodeIgniter


Image representing Facebook Connect as depicte...
Image via CrunchBase

I’ve tried to add Facebook Login button to my CodeIgniter application. Since I’m new to both CodeIgniter and Facebook Connect, at least from development aspect, I have looked for existing tutorials.  After research I found that Danny  Herrarn tutorial is the best one.

However, I faced a couple of  issues, to which, I found no answer neither in the post or the comments and discussions beneath it.

That said, if you’re new, go to Danny  Herrarn tutorial first. If things were not working for you, get back to this page and look at the different issues I faced and how I fixed them:

  1. Blank Page:  the reason for this is definitely that you’re having and error. So why you might not be getting any errors though you’ve error_reporting(E_ALL)   in your index.php? the answer is simple. The sitting in PHP.ini has more precedence. To make sure you get errors on the screen add the following line
    ini_set(“display_errors”,”1″);Your code in index.php should look something like below:
    Most probably the errors are related to you not auto loading the session library and not defining an encryption key; which is required for session management. In your config.php make sure that $config[‘encryption_key’] has a value. Then go to autoload.php and make sure  to add session to  $autoload[‘libraries’]  array.
  2. Page does not refresh after login: One have to manually refresh the page after login on Facebook to have the page refreshed. Though my understanding is that this is the default behavior in the newest version of Facebook connect, it did not work with me! I have to define a  login function callback to handle that. The code below worked perfectly with me:<div id="fb-root"></div>
    <script>
    window.fbAsyncInit = function() {
    FB.init({
    appId: 'YOUR APP ID',
    cookie: true,
    xfbml: true,
    oauth: true
    });
    FB.Event.subscribe('auth.logout', function(response) {
    return false;
    });
    };
    (function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol +
    '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
    }());
    function fbLogin() {
    window.location.reload();
    }<</script>
    <fb:login-button length="long" autologoutlink="false" onlogin="fbLogin();"></fb:login-button>

Finally, I have to admit, I am new to both CodeIgniter and Facebook Login and I was just interested to have things working. This might not be the optimal way to do it, but if you just want things to get working, I believe it serves the purpose J.

4 thoughts on “GUIDE: Facebook Login with CodeIgniter

  1. pawel

    Hey I have the issue while doing the function $this->facebook->getUser(); I loaded the library and now it wants me to create object one more time?
    It’s error: Fatal error: Call to a member function getUser() on a non-object in /home/dev/system/application/models/facebook_model.php on line 18

    1. I wrote this long time ago :).. not sure if I still have the code. Let me check.

      One this that I can tell from the error is that FB object is not initated right. There might be a problem in your APP ID or something like that

  2. I’m really inspired with your writing skills and also with the format in your blog. Is that this a paid theme or did you customize it your self? Anyway stay up the excellent quality writing, it is uncommon to peer a great weblog like this one nowadays..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: