Codeigniter如何包含facebook sdk初始化?

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'  => '201637766943985',
  'secret' => '4e70dc1dbfc4787e81bec0d7b57d6a1c',
));


$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'  => '201637766943985',
      'secret' => '4e70dc1dbfc4787e81bec0d7b57d6a1c',
    ));


    $user = $facebook->getUser();

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

$this->load->view('fb_init.php');

However, The problem is the remaining code of facebook php sdk cannot refer to $user 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) {
        error_log($e);
        $user = null;
      }
    }

$loginUrl = $facebook->getLoginUrl(array(  
    "redirect_uri"=>"http://www.club55.net/login",   
    "scope"=>"email"
  ));

How could I do?

Try this, in your fb_init.php.

$data['user'] = $facebook->getUser();

And in your view file.

$this->load->view('fb_init.php', $data);

if ($user) {
...

you can do it using javascript it will open facbook login popup on our site will not redirect to facebook site.

please follow this --

<html>
<body>
<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '717505658366417', 
      status     : true,
      cookie     : true, 
      xfbml      : true  
    });
  };

    function Login()
    {

        FB.login(function(response) {
           if (response.authResponse) 
           {
                getUserInfo();
            } else 
            {
             console.log('User cancelled login or did not fully authorize.');
            }
         },{scope: 'email,user_photos,user_videos'});

    }

  function getUserInfo() {
        FB.api('/me', function(response) {
      var str="<b>Name</b> : "+response.name+"<br>";
          str +="<b>Link: </b>"+response.link+"<br>";
          str +="<b>Gender:</b> "+response.gender+"<br>";
          str +="<b>id: </b>"+response.id+"<br>";
          str +="<b>Email:</b> "+response.email+"<br>";
          str +="<input type='button' value='Get Photo' onclick='getPhoto();'/>";
          str +="<input type='button' value='Logout' onclick='Logout();'/>";
          document.getElementById("status").innerHTML=str;

    });
    }
    function getPhoto()
    {
      FB.api('/me/picture?type=normal', function(response) {

          var str="<br/><b>Pic</b> : <img src='"+response.data.url+"'/>";
          document.getElementById("status").innerHTML+=str;

    });

    }
    function Logout()
    {
        FB.logout(function(){document.location.reload();});
    }

  (function(d){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);
   }(document));

</script>
<a href="javscript:void(0);" onclick="Login()">Login</a>
</body>
</html>

It`s a universal code we can use this code in any languages or in any technology, hope it will work.