• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to include facebook sdk initial?


Hi I'm beginner in Codeigniter. I have 4 view page that have facebook login (1. login page 2.register page 3.login special member page 4.register special member page)
And all 4 views file I have to initial facebook php sdk like this

<?php require_once(APPPATH.'libraries/facebook/src/facebook.php');
$facebook = new Facebook(array(
 'appId'  => 'xxxxxxxxxxxxxx',
 'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',

$user = $facebook->getUser();

It's very annoying that one day I have to change appID and appsecret, I have to change it in 4 view files.
so I create new view file name "fb_init.php" that contain code

<?php require_once(APPPATH.'libraries/facebook/src/facebook.php');
   $facebook = new Facebook(array(
     'appId'  => 'xxxxxxxxxxxxxxxx',
     'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',

   $user = $facebook->getUser();

and then in 4 view files I include fb_init.php by write


However, The problem is the remaining code of facebook php sdk cannot refer to 


 variable. the remaining code of facebook sdk is here.

   if ($user) {
     try {
       // Proceed knowing you have a logged in user who's authenticated.
       $user_profile = $facebook->api('/me?fields=id,email,name,first_name,last_name,picture'); //ต้องมีหลัง /me ไม่งั้นประวัติมาไม่ครบ
     } catch (FacebookApiException $e) {
       $user = null;

$loginUrl = $facebook->getLoginUrl(array(  

How could I do?

You need a library. Then return the $user.

Create a file in libraries called Facebook_library.php

In it do something 'like' this:

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

Facebook_library {
protected $appId '201637766943985';
    protected $secret '4e70dc1dbfc4787e81bec0d7b57d6a1c';
public function __construct()
         // only needed to access CI objects
         // like $CI->load->library('form_validation');
         $this->CI =& get_instance();
public function get_user($user_id$type='') {
         $facebook = new Facebook(array(
              'appId'  => $this->appId,
              'secret' => $this->secret,
         $user $facebook->getUser();
          return $user

Then in your controller or any controller or any time you need it:

PHP Code:
$user $this->facebook_library->get_user(); 

I have just typed this out and not done it anywhere so it is probably full of bugs as I typed it. But you can read about libraries here: https://www.codeigniter.com/user_guide/g...aries.html

They are extraordinarily useful. And you can keep your code very DRY. And as with any class you can have many functions like delete_user, authorise_user, get_user_profile or whatever. And if your FB details change you only need to change them in one place. In fact, you could put them into the config file and call them as config items in the library.

Alternatively you can do similar in a model, and then you do not need to worry about the CI super object.

Hope that helps in some way,

Best wishes,


Edit: PS There are lots of resources, advice, help files and example codes if you just google for something like "codeigniter facebook sdk library"

Particularly bad idea to post a Facebook app key on a public forum. Just sayin' Undecided

I presumed he had just done some random representative numbers. But if they are genuine, not a good idea at all.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.