在codeigniter上使用facebook登录api无法正常工作

时间:2014-06-07 10:32:54

标签: facebook codeigniter

嗨我有这个facebook登录我整合在我的网站我已经在facebook创建了一个名为test的应用程序,我有api密钥和秘密。

我正在使用codeigniter框架。

我的问题是,当我试图回显$this->facebook->getUser();时,它将显示为0。

我已经在codeigniter库中添加了facebook SDK,我将其添加到application / libraries文件夹中。这是我的登录控制器。

有人可以帮我解决这个问题吗?非常感谢任何帮助。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller{
  public $user = null;
  public function __construct(){
    parent::__construct();
    $this->load->library('form_validation');
    $this->load->model('users_model', 'um');
    $this->load->library('session');
    $this->load->library('facebook', 
                    array(
                      'appId'=>'MY APP ID', 
                      'secret'=>'MY SECRET ',
                    ));

    $this->user = $this->facebook->getUser();
    echo $this->facebook->getUser(); exit;
    if($this->session->userdata('loggedIn')){
      redirect('homepage');
    }
  }

  public function fb(){  
    if($this->user){
      try{
        echo "hi";
        $user_profile = $this->facebook->api('/me');
        echo "<br />";
        print_r($user_profile);exit;


      }catch(FacebookApiException $e){
        print_r(e);
        $user = null;
      }
    }

    if($this->user){
      //$logout = $this->facebook->getLogoutUrl();
      //echo $logout;
    }else{
      //$login = $this->facebook->getLoginUrl();
      //echo $login;
    }

  }

  public function check_database($password){
    $email = $this->input->post('email');
    $result = $this->um->login($email, $password);
    if($result){
      $sess_array = array();
      foreach($result as $row){
        $sess_array = array(
                    'id'=>$row->id,
                    'username'=>$row->username  
                  );
        $this->session->set_userdata('loggedIn', $sess_array);
      }
      return true;
    }else{
      $this->form_validation->set_message('check_database','Invalid Email or Password.');
      return false;
    }
  } 

  public function verifyLogin(){
    $this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
    if($this->form_validation->run() == FALSE){
      $this->data['title'] = 'Login';

      $this->load->view('templates/header', $this->data);
      $this->load->view('pages/login');
      $this->load->view('templates/footer');
    }else{
      redirect('homepage');
    } 
  }

    public function index(){
    $this->data['title'] = 'Login';
    $this->load->view('templates/header', $this->data);
    $this->load->view('pages/login');
    $this->load->view('templates/footer');
    }
}

0 个答案:

没有答案