1

脸书配置

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

<?php
   $config['appId'] = APP_ID;
   $config['secret'] = APP_SECRET;
?>

控制器代码:

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

class Facebooklogin extends CI_Controller 
{
    public function Main()
    {
        parent::__construct();
        parse_str( $_SERVER['QUERY_STRING'], $_REQUEST );
        $CI = & get_instance();
        $CI->config->load("facebook",TRUE);
        $config = $CI->config->item('facebook');
        $this->load->library('Facebook', $config);
    }

    function index()
    {
        // Try to get the user's id on Facebook
        $userId = $this->facebook->getUser();

        // If user is not yet authenticated, the id will be zero
        if($userId == 0)
        {
            // Generate a login url
            $data['url'] = $this->facebook->getLoginUrl(array('scope'=>'email'));
            $this->load->view('signin', $data);
        } 
        else 
        {
            // Get user's data and print it
            $user = $this->facebook->api('/me');
            print_r($user);
        }
    }
}

?>

查看文件

<a href="<?php echo $url;?>">
    <button class="btn btn-mini btn-facebook">
       <i class="icon-facebook"></i>
       | Connect with Facebook
    </button>
</a>

我已经集成了这个登录 api 代码。当我尝试登录时,该 url 不会重定向到 facebook 登录页面。我不知道如何重定向 url。我还添加了配置文件。

4

1 回答 1

1

Change your code to something like:

...
$login_url = $this->facebook->getLoginUrl(array('scope'=>'email'));
header( 'Location: ' . $login_url );
die();
...

Basically, do a header redirect straight to the login page if they're not logged in. Alternatively, you can do a JavaScript redirect in your signin view:

<script type="text/javascript">
window.location = '<?php echo $url; ?>';
</script>
于 2013-09-03T14:20:03.437 回答