我试图在我的项目中使用facebook登录。为此,我在codeigniter中使用PHP SDK。
一切正常,我可以登录并将数据检索到我的页面。
但是我不明白为什么注销不起作用,因为当我点击注销链接时没有任何事情发生。
我在互联网上搜索了很多东西并尝试过。
我不知道现在做了什么......希望有人能帮助我。
这是我的代码:
模型/ Facebook_model
<?php
class Facebook_model extends CI_Model {
public function __construct()
{
parent::__construct();
$config = array(
'appId' => 'xxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxx',
'fileUpload' => true, // Indicates if the CURL based @ syntax for file uploads is enabled.
);
$this->load->library('Facebook', $config);
$user = $this->facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
$profile = null;
if($user)
{
try {
// Proceed knowing you have a logged in user who's authenticated.
$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' => site_url('home') // URL where you want to redirect your users after a successful login
'redirect_uri' => base_url()
)
),
'logoutUrl' => $this->facebook->getLogoutUrl(),
);
}
}
controller / mycontroller.php(我有其他功能的搜索功能)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Concertos extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('Facebook_model');
}
function topsecret()
{
$fb_data = $this->session->userdata('fb_data');
if((!$fb_data['uid']) or (!$fb_data['me']))
{
redirect('home');
}
else
{
$data = array(
'fb_data' => $fb_data,
);
$this->load->view('home', $data);
}
}
public function search($location)
{
$fb_data = $this->session->userdata('fb_data');
$data ['fb_data'] = $fb_data;
$this->load->view('home',$data);
}
查看/ 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 src="https://graph.facebook.com/<?php echo $fb_data['uid']; ?>/picture" alt="" class="pic" />
<p>Hi <?php echo $fb_data['me']['name']; ?>,<br />
<a href="<?php echo $fb_data['logoutUrl']; ?>">logout</a></p>
<?php endif;
?>
答案 0 :(得分:1)
由于您正在创建会话,因此当您注销时需要在注销功能中销毁它。然后你需要链接到这个功能。
function logout()
{
$this->CI->session->sess_destroy();
// do more thing you want to do such as redirect
}
答案 1 :(得分:0)
我认为没有错。你知道getLogoutUrl会从facebook.com注销用户吗?因此,如果想要仅从应用程序注销用户,我认为它需要一些其他本机代码。