且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Codeigniter 2.1.2和Facebook-PHP-SDK 3.1.1

更新时间:2023-12-03 10:43:04

Facebook PHP SDK使用$ _REQUEST变量,而CodeIgniter为了安全原因清除$ _REQUEST变量。



为了解决这个问题,你可以将$ _GET的内容复制到$ _REQUEST在调用getUser()之前:

  $ _ REQUEST + = $ _GET; 
$ this-> facebook-> getUser();

另一个解决方案是修改Facebook PHP SDK使用$ _GET而不是$ _REQUEST。 p>

希望这有助于。


I'm developing a simple authentication trough Codeigniter 2.1.2 and Facebook-PHP-SDK 3.1.1, but I have no idea why it's now working.

I've downloaded all files from the src folder (https://github.com/facebook/facebook-php-sdk/tree/master/src) into my libraries.

The login seems to be working, but when the user is redirected back to my website, the Facebook getUser() function always returns 0!

<?php

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

class Bloompit extends CI_Controller
{
    public $user;

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('session');
        $this->load->library('facebook', array(
            'appId' => '149275358530064',
            'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxx'
        ));
        $this->load->model('bloompit_model');
        $this->user = $this->session->userdata('user');
    }

    public function index()
    {
        $data['title'] = 'Bloompit';
        $data['page'] = 'home';
        $this->load->view('view', $data);
    }

    public function login()
    {
        if ($this->user) {
            redirect(site_url(), 'location');
        } elseif ($this->facebook->getUser()) {
            try {
                $facebook_user = $this->facebook->api('/me');
                $this->session->set_userdata('user', array(
                    'name' => $facebook_user['name']
                ));
            } catch (FacebookApiException $e) {
                log_message('error', $e);
                redirect($this->facebook->getLoginUrl(array(
                    'scope' => 'email',
                    'redirect_uri' => site_url('login')
                )), 'location');
            }
        } else {
            $data['facebook_login_url'] = $this->facebook->getLoginUrl(array(
                'scope' => 'email',
                'redirect_uri' => site_url('login')
            ));
            $data['title'] = 'Entrar';
            $data['page'] = 'login';
            $this->load->view('view', $data);
        }
    }

    public function logout() {
        session_destroy();
        redirect($this->facebook->getLogoutUrl(array(
            'next' => site_url()
        )), 'location');
    }
}

The Facebook PHP SDK utilizes the $_REQUEST variable while CodeIgniter purges the $_REQUEST variable for security reasons.

To get around this, you may copy the content of $_GET to $_REQUEST before calling getUser():

$_REQUEST += $_GET;
$this->facebook->getUser();

Another solution would be to modify the Facebook PHP SDK to use $_GET instead of $_REQUEST.

Hope this helps.