Using Facebook PHP-SDK 3.x to register/login user with Codeigniter 2.1.0

后端 未结 4 2642
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 07:10

Going over the Facebook API and I\'m a bit confused on the right approach. I want users to skip registration, or auto-register them if they sign in with Facebook. So if th

相关标签:
4条回答
  • 2020-12-09 07:22

    After getting the $user, check session parameters with print_r($_SESSION). After that, check some variables by if(isset($_SESSION['some session var'])). If this condition is true, then navigate to your main page with header("location:main.php"); exit;

    0 讨论(0)
  • 2020-12-09 07:37

    I suggest you read the framework documentation. Adding a library to CodeIgniter is not a hard task. And Facebook library is no exception.

    Here's a quick integration I've just come up with:

    1.create a config file:application/config/facebook.php

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    $config['appId'] = 'app_id';
    $config['secret'] = 'app_secret';
    

    2.place the sdk files in the libraries folder application/libraries/ and rename the facebook.php file to Facebook.php and replace the php tag with this:

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

    3.in your controller load the config file and then load the Facebook library:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Welcome extends CI_Controller {
    
        public function __construct()
        {
            parent::__construct();
            // Your own constructor code
            $CI = & get_instance();
            $CI->config->load("facebook",TRUE);
            $config = $CI->config->item('facebook');
            $this->load->library('facebook', $config);
        }
    
        public function index()
        {
            $user = $this->facebook->getUser();
            if($user) {
                try {
                    $user_info = $this->facebook->api('/me');
                    echo '<pre>'.htmlspecialchars(print_r($user_info, true)).'</pre>';
                } catch(FacebookApiException $e) {
                    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
                    $user = null;
                }
            } else {
                echo "<a href=\"{$this->facebook->getLoginUrl()}\">Login using Facebook</a>";
            }
        }
    }
    

    Now in the constructor method, you have just initialized the Facebook library (sdk) and it can be accessed by using: $this->facebook.

    Notes:

    • You can always use an existing library, just google it
    • A common practice is to extend the core Controller class and add the Facebook library initialization there.
    • Or create another library, extend the Facebook library, load the config file there and then autoload this new library.
    0 讨论(0)
  • 2020-12-09 07:39

    You can try using my extension http://github.com/deth4uall/Facebook-Ignited it is set up to work with Facebook. Just update the config file and it will allow you to do what you need to do, as well as some additional methods that I added myself.

    0 讨论(0)
  • 2020-12-09 07:40

    do not know why, but it always runs this line:

    echo "<a href=\"{$this->facebook->getLoginUrl()}\">Login using Facebook</a>";

    This is the error message:

    couldn't connect to host

    But this worked.

    0 讨论(0)
提交回复
热议问题