用facebook登录CodeIgniter无法正常工作 [英] login with facebook CodeIgniter not working
本文介绍了用facebook登录CodeIgniter无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我跟着下面的内容
Danny Herran [ ^ ]
这是我的代码
控制器
home.php
<? php
ini_set(display_errors,1);
class Home扩展CI_Controller
{
function __construct()
{
parent :: __ construct();
$ this-> load-> model('Facebook_model');
$ this-> load-> library('session');
}
函数索引()
{
$ fb_data = $ this-> session-> userdata('fb_data');
if((!$ fb_data ['uid'])或(!$ fb_data ['me']))
{
//如果这是受保护的部分,需要用户身份验证
//您可以将用户重定向到其他地方
//或采取您需要的任何其他操作
redirect('login');
}
其他
{
$ data = array(
'fb_data'=> $ fb_data,
);
$ this-> load-> view('home',$ data);
}
}
}
?>
< br $> b $ b型号
facebook_model.php
<?php
类Facebook_model扩展CI_Model
{
公共函数__construct()
{
parent :: __ construct();
$ config = array(
'appId'=>'828846557141612',
'secret'=>'7913e12f9a5d746f10a35c5c176cce42',
'fileUpload'=> true,/ /表示CURL
$ b);
$ this-> load-> library('Facebook',$ config);
$ user = $ this-> facebook-> getUser();
$ profile = null;
if($ user)
{
try
{
$ profile = $ this-> facebook-> api('/ me?田= ID,名称,链接,电子邮件');
}
catch(FacebookApiException $ e)
{
error_log($ e);
$ user = null;
}
}
$ fb_data =数组(
'我'=> $个人资料,
'uid'=> $ user,
'loginUrl'=> $ this-> facebook-> getLoginUrl(
array(
'cope'=>'email,user_birthday,publish_stream',// app权限
'redirect_uri'=>'http://tawrny.com/demo/jobs/'//你想在
之后重定向你的用户的URL)
),
'logoutUrl '=> $ this-> facebook-> getLogoutUrl(),
);
$ this-> session-> set_userdata('fb_data',$ fb_data);
}
}
?>
查看
home.php
<? php if(!$ fb_data ['me']): ? >
请使用您的FB帐户登录:< a href = <?php echo $ fb_data ['loginUrl'];?> > 登录< / a >
<! - 或者您可以使用XFBML - >
< div class = fb-login-button data-show-faces = false data-width = 100 data-max-rows = 1 数据范围 = email,user_birthday,publish_stream > ; < / div >
<? php else: ? >
< img < span class =code-attribute> class = pic alt = src = https:// graph.facebook.com/<?php echo $ fb_data ['uid']; ?> / picture / > ;
您好<? php echo $ fb_data ['me'] ['name']; ? > ,
< a href = <?php echo site_url('topsecret'); ?> > 您可以访问最高机密页< / a > 或< a href = <?php echo $ fb_data ['logoutUrl']; ?> > logout < / a >
<? php endif; ? >
当我导航到我的网站时,显示以下内容
此网页有一个重定向循环
并且没有任何事情发生
解决方案
this-> load-> model('Facebook_model');
this-> load-> library('session');
}
function index()
{
fb_data =
i have followed the following tut
Danny Herran[^]
here is my code
controller
home.php
<?php ini_set("display_errors","1"); class Home extends CI_Controller { function __construct() { parent::__construct(); $this->load->model('Facebook_model'); $this->load->library('session'); } function index() { $fb_data = $this->session->userdata('fb_data'); if((!$fb_data['uid']) or (!$fb_data['me'])) { // If this is a protected section that needs user authentication // you can redirect the user somewhere else // or take any other action you need redirect('login'); } else { $data = array( 'fb_data' => $fb_data, ); $this->load->view('home', $data); } } } ?>
model
facebook_model.php
<?php class Facebook_model extends CI_Model { public function __construct() { parent::__construct(); $config=array( 'appId' => '828846557141612', 'secret' => '7913e12f9a5d746f10a35c5c176cce42', 'fileUpload' => true, // Indicates if the CURL ); $this->load->library('Facebook',$config); $user=$this->facebook->getUser(); $profile = null; if($user) { try { $profile=$this->facebook->api('/me?fields=id,name,link,email'); } catch(FacebookApiException $e) { error_log($e); $user = null; } } $fb_data=array( 'me' => $profile, 'uid' => $user, 'loginUrl' => $this->facebook->getLoginUrl( array( 'scope' => 'email,user_birthday,publish_stream', // app permissions 'redirect_uri' => 'http://tawrny.com/demo/jobs/' // URL where you want to redirect your users after ) ), 'logoutUrl' => $this->facebook->getLogoutUrl(), ); $this->session->set_userdata('fb_data', $fb_data); } } ?>
view
home.php
<?php if(!$fb_data['me']): ?>
Please login with your FB account: <a href="<?php echo $fb_data['loginUrl']; ?>">login</a>
<!-- Or you can use XFBML -->
<div class="fb-login-button" data-show-faces="false" data-width="100" data-max-rows="1" data-scope="email,user_birthday,publish_stream"></div>
<?php else: ?>
<img class="pic" alt="" src="https://graph.facebook.com/<?php echo $fb_data['uid']; ?>/picture" />
Hi <?php echo $fb_data['me']['name']; ?>,
<a href="<?php echo site_url('topsecret'); ?>">You can access the top secret page</a> or <a href="<?php echo $fb_data['logoutUrl']; ?>">logout</a>
<?php endif; ?>
when i navigate to my site url the following shown
This webpage has a redirect loop
and no thing happens
解决方案
this->load->model('Facebook_model');
this->load->library('session'); } function index() {
fb_data =
这篇关于用facebook登录CodeIgniter无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文