Codeigniter 2.1.2和Facebook-PHP-SDK 3.1.1 [英] Codeigniter 2.1.2 and Facebook-PHP-SDK 3.1.1
问题描述
我正在通过Codeigniter 2.1.2和Facebook-PHP-SDK 3.1.1开发一个简单的身份验证,但我不知道为什么它现在工作。
我已经从src文件夹(https://github.com/facebook/facebook-php-sdk/tree/master/src)下载所有文件到我的库。
登录似乎工作,但当用户重定向回我的网站时,Facebook的getUser()函数总是返回0!
<?php
if(!defined('BASEPATH'))
exit允许');
类Bloompit扩展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 );
} 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');
}
}
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.
这篇关于Codeigniter 2.1.2和Facebook-PHP-SDK 3.1.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!