简体   繁体   English

Codeigniter 2.1.2和Facebook-PHP-SDK 3.1.1

[英]Codeigniter 2.1.2 and Facebook-PHP-SDK 3.1.1

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. 我正在通过Codeigniter 2.1.2和Facebook-PHP-SDK 3.1.1开发一个简单的身份验证,但我不知道为什么它现在正在工作。

I've downloaded all files from the src folder (https://github.com/facebook/facebook-php-sdk/tree/master/src) into my libraries. 我已经将src文件夹(https://github.com/facebook/facebook-php-sdk/tree/master/src)中的所有文件下载到我的库中。

The login seems to be working, but when the user is redirected back to my website, the Facebook getUser() function always returns 0! 登录似乎正常,但当用户重定向回我的网站时,Facebook getUser()函数始终返回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. Facebook PHP SDK使用$ _REQUEST变量,而CodeIgniter出于安全原因清除$ _REQUEST变量。

To get around this, you may copy the content of $_GET to $_REQUEST before calling getUser(): 要解决此问题,您可以在调用getUser()之前将$ _GET的内容复制到$ _REQUEST:

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

Another solution would be to modify the Facebook PHP SDK to use $_GET instead of $_REQUEST. 另一个解决方案是修改Facebook PHP SDK以使用$ _GET而不是$ _REQUEST。

Hope this helps. 希望这可以帮助。

You are using only function name in your site_url() function which is incorrect you must specify the controllers name. 您在site_url()函数中仅使用函数名称,这是不正确的,您必须指定控制器名称。

Try this one: 试试这个:

$this->facebook->getLoginUrl(array(
                'scope' => 'email',
                'redirect_uri' => site_url('bloompit/login')
            ));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM