我正在使用this Facebook lib用于CodeIgniter,因此用户可以使用Facebook登录 . 在 生产环境 上工作得很好,但在localhost上却不行 . 困扰我的是它曾经在localhost上正常工作,但突然停止工作 .
我一直在寻找一段时间,我期待这与CI的会话库有关,但我不是百分百肯定 .
Background info
-
我正在使用PHP 5.5.10和Chrome 41.0.2272.118运行MAMP
-
本地我've configured the httpd.conf in MAMP so my local url is a '真正的'URL如www.example-local.net和Apache端口是80
视图中的 -
if($this->session)
返回true . 编辑;我只是为了测试这是否有效 -
生产环境 和开发代码完全相同,除了Facebook API密钥和秘密,因为我在本地使用测试应用程序 . 如..所示;之前它工作得很好,我没有改变我的测试应用程序设置 .
-
编辑:我将我的TLD配置为FB测试应用中的应用域和站点URL . 同样,这似乎不是问题所在
The problem
(Facebook)库文件中的
-
if($this->session)
返回false . 编辑;这似乎是个问题 . 所以这在视图中工作,而不是在库中 . 关于它的奇怪之处在于它之前正在工作(没有代码更改) -
因此,似乎库没有在会话userdata中设置
fb_boken
并且's why I suspect there'设置$this->session
时出错了 -
我确实搬到了不同的时区(仅1小时),我在某处读到这可能会导致问题?编辑;我've been back in the timezone where it worked previously and it'仍然无法正常工作
What I already tried and did not work
-
在
config.php
设置$config['sess_expiration']
为高值(例如999999)而不是7200 -
在
config.php
将$config['cookie_domain']
更改为localhost网站(http://www.example-local.net
,尝试使用和不使用尾部斜杠)而不是空(“”) -
在
config.php
中将$config['sess_match_useragent']
更改为FALSE
而不是TRUE
-
用PHP的原生会话替换app / system / library / Session.php,如here所述
-
清除我的所有Cookie和浏览数据
Additional info on config.php
$config['cookie_prefix'] = "";
$config['cookie_domain'] = "";
$config['cookie_path'] = "/";
$config['cookie_secure'] = FALSE;
我希望使用PHP的本机会话可以解决问题,但事实并非如此 . 不知道我还能尝试什么 .
For reference the full Facebook.php library file code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( session_status() == PHP_SESSION_NONE ) {
session_start();
}
require_once( APPPATH . 'libraries/facebook/Facebook/GraphObject.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/GraphSessionInfo.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookSession.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookCurl.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookHttpable.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookCurlHttpClient.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookResponse.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookSDKException.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookRequestException.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookAuthorizationException.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookRequest.php' );
require_once( APPPATH . 'libraries/facebook/Facebook/FacebookRedirectLoginHelper.php' );
use Facebook\GraphSessionInfo;
use Facebook\FacebookSession;
use Facebook\FacebookCurl;
use Facebook\FacebookHttpable;
use Facebook\FacebookCurlHttpClient;
use Facebook\FacebookResponse;
use Facebook\FacebookAuthorizationException;
use Facebook\FacebookRequestException;
use Facebook\FacebookRequest;
use Facebook\FacebookSDKException;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\GraphObject;
class Facebook {
var $ci;
var $helper;
var $session;
public function __construct() {
$this->ci =& get_instance();
FacebookSession::setDefaultApplication( $this->ci->config->item('api_id', 'facebook'), $this->ci->config->item('app_secret', 'facebook') );
$this->helper = new FacebookRedirectLoginHelper( $this->ci->config->item('redirect_url', 'facebook') );
if ( $this->ci->session->userdata('fb_token') ) {
$this->session = new FacebookSession( $this->ci->session->userdata('fb_token') );
// Validate the access_token to make sure it's still valid
try {
if ( ! $this->session->validate() ) {
$this->session = false;
}
} catch ( Exception $e ) {
// Catch any exceptions
$this->session = false;
}
} else {
try {
$this->session = $this->helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
} catch(\Exception $ex) {
// When validation fails or other local issues
}
}
if ( $this->session ) {
$this->ci->session->set_userdata( 'fb_token', $this->session->getToken() );
$this->session = new FacebookSession( $this->session->getToken() );
}
}
/*
|--------------------------------------------------------------------------
| Login URL
|--------------------------------------------------------------------------
*/
public function get_login_url() {
return $this->helper->getLoginUrl( $this->ci->config->item('permissions', 'facebook') );
}
/*
|--------------------------------------------------------------------------
| Logout URL
|--------------------------------------------------------------------------
*/
public function get_logout_url() {
if ( $this->session ) {
return $this->helper->getLogoutUrl( $this->session, site_url() );
}
return false;
}
/*
|--------------------------------------------------------------------------
| Get user data
|--------------------------------------------------------------------------
*/
public function get_user() {
if ( $this->session ) {
try {
$user = (new FacebookRequest( $this->session, 'GET', '/me' ))->execute()->getGraphObject()->asArray();
return $user;
} catch(FacebookRequestException $e) {
return false;
}
}
}
}
2 回答
您正在使用的库是核心CodeIgniter代码库的外部库,因此无法以正常方式与本机函数通信;而不是使用
$this
作为对CodeIgniter实例的引用,在库中你应该使用$this->ci
以便访问会话数据,你的代码看起来像:这在CodeIgniter文档中有更详细的解释here
在视图中使用
$this->facebook->session
而不是$this->session
它将工作;)