简体   繁体   中英

Facebook login api + codeigniter

Facebook Config

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

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

Controller Code:

<?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);
        }
    }
}

?>

View File

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

I have integrated this login api code. when am trying to login, the url does not redirect to the facebook login page. I dont know how to redirect the url.Also i have added configuration file.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM