Integrating Facebook PHP SDK is not that hard, it only takes few minutes to do that. But its completely different story when you are integrating it with CodeIgniter.

Thats because Facebook PHP SDK takes input from $_REQUEST variable while CodeIgniter purges the $_REQUEST variable for security reasons. Other important reason is theres no dedicated library available for it (while there are few available for Twitter) and there is vast range of solutions if you search for it but no proper solution (atleast I didn’t find one).

So I decided to create one and worked on it last week.

Facebook PHP SDK for CodeIgniter on Github

There are four folders config, controllers, libraries and views. All you need to do is, copy these folders to application folder of your CI installation.

Then replace your facebook application id and secret in facebook.php file under config folder:

And thats it. Try accessing domain.com/welcome/login (localhost if testing locally).

 

Few things to double check:

  1. Your FB application is not on Sandbox mode.
  2. Your website domain (or localhost if testing locally) is listed in app domains.
  3. Website with Facebook Login is enabled and pointed to your domain.
  4. App type is set to Web under advance settings.
  5. Client OAuth Login is enabled under advance settings.

 

31 Responses

  1. I followed the instructions. After logged in, FB redirects back to welcome/login with a code; i.e. https://localhost/welcome/login?code=AQDa4Hs3RiPkHHFpLQqIx-hR5j6Yz0brrZOcAFEJ8oNdugDHRv-GyttYaiOcWuUS31BJ_0F_IDPxJxkTSoHPJ-jXnfSono0gBw5ZyAxI8kVPR6c0hm8fjbbdUOotru1cSspGOSeH5s936fQnhpkGYKakecxQU33lNYXSIYbPV4dR33BheCw8ShBcVjsq61gULH_fKNXIi-NZqnv4XpOYqlBRp4YGokRBHCHKQ26IZ0xExzgkllW5ixCfNt2NDYuzwsZLnaI-YxjlIJMLFKIu-7SRvzpomjaCKARa6lSeE9ii6tS1pYFHuDrBT3yJA9CtZ7c&state=f2322bf177eb8bac7092668b64d59715#_=_. However, the base_facebook.php seems not to pick it up and gets the access token. As a result, getUser() always returns null (the welcome pages never shows the user profile). Something I did wrong? Also, I still see $_REQUEST in the base_facebook.php which is the main problem in CI.
    Thanks,

    1. Hi Frank, $_GET is being assigned to $_REQUEST explicitly. I’m using this library on few projects and its working quite well. Returning url looks fine too. I believe, the problem lies within your facebook application config. Please double check these things:

      Your FB application is not on Sandbox mode.
      Your website domain (or localhost if testing locally) is listed in app domains.
      Website with Facebook Login is enabled and pointed to your domain.
      App type is set to Web under advance settings.
      Client OAuth Login is enabled under advance settings.

      And remember, It takes some time to update these changes.

      Regards

  2. I have done whatever it needs to run this. It asks me for permissions set the “code” in the URL, yet the user is empty. What may the problem be?

    1. What user information are you trying to get from FB? I think, you are trying to get something your application doesn’t have permission for. Go to App Details section on FB and configure App Center permissions. You even need to add “email” under User and Friend Permissions in order to get user email address from facebook.

      After you configure permissions (Or if you think, you have done it right already), delete your application from “Manage applications” from your FB account and try again. It might still be using old permissions only.

      1. Thanks for your reply I did what you told me to do. Now I’m getting this error:
        Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in

  3. Fatal error: Call to undefined method Facebook::setSession() in /home/demo/public_html/application/controllers/welcome.php on line 61

    1. Hey Dheerendra, I have just updated the library and included demo as well. Download a fresh copy from Github repo and it will work fine. Cheers!

  4. some time when i click on login link,then it show white screen,and after some time its working,how can it solve..?

    1. Okay, no need to answer that one. It’s fixed.

      But now there’s another one – Login button shows up, I press login, FB asks me to press “Ok” or “Cancel”, I press Ok and nothing happens. Why?

  5. Hi!! nice code but i hace a big problem, i’m using your library and check all in the FB setup, all are correct, now in the code i set all ok, but when i do the login, i have always the $user = 0… do you can help me?

  6. Hi,

    I have downloaded the files from github and trying to use your code but somehow I am getting client id 0 in the login url and because of empty client id I am getting Invalid app id error on facebook.

    Please advise what thing I am missing asap.

    Thanks

  7. successfully able to view login page but when i click on login.,

    showing 404 not found error,

    login button points to: “http://localhost/codeignitor/codyapp/index.php/welcome/%3C?=%20$login_url%20?%3E”

    when i inspected your demo site, its showing :

    “https://www.facebook.com/dialog/oauth?client_id=306067306207563&redirect_uri=https%3A%2F%2Fpuneetk.com%2Ffacebook-php-codeigniter%2Findex.php%2Fwelcome%2Flogin&state=c2635519685cbc4813bd9e0be4bbd121&sdk=php-sdk-3.2.3&scope=email”

    help me solve this

    thanks

  8. thats great! initialy had some issues..bot working now! can u suggest me how to save user profile in database for further refrence..

    i have added users table…it should be like…if this user exist, update pic..(fetch pic and update it in my uploads/user folder and update the filepath to table)

  9. by the way, i suggest you to install jetpack ( and activate “follow comments via mail”) or comment system like disqus, so that i can follow up

  10. Hi Puneet,

    First of all thanks for such a wonderful tutorial. I am able to successfully login to the website using fb. However I am not able to store the details into Mysql database.

    It will be gr8 if you can guide me with a solution.

    Thanks,

    Utpal

  11. Hi

    I am facing this error:

    A PHP Error was encountered

    Severity: Notice

    Message: Undefined variable: login_url

    Filename: views/login.php

    Line Number: 24

    ” class=”btn btn-lg btn-success btn-block” role=”button”>Login

  12. hii i implemented a code and get a response id and name but i want some more information like email etc.. how it can be possible

  13. hii i implemented a code and get a some information like id,first_name , email, etc.. but not birthday and mobile no. how it can be possible

Leave a Reply

Your email address will not be published. Required fields are marked *


Notice: Use of undefined constant STOPSPAM_PLUGIN_VERSION - assumed 'STOPSPAM_PLUGIN_VERSION' in /home/webpilla/puneetk.com/wp-content/plugins/stop-spam/stop-spam.php on line 36

Notice: Use of undefined constant STOPSPAM_PLUGIN_VERSION - assumed 'STOPSPAM_PLUGIN_VERSION' in /home/webpilla/puneetk.com/wp-content/plugins/stop-spam/stop-spam.php on line 40