简体   繁体   中英

Facebook connect with codeigniter redirect in loop after login

I am trying to connect facebook using codeigniter. If i am connecting fist time,then I can see permission popup but after I authorise permission i am getting multiple redirect loop error.

If I am already logged in and authorised site then why it is not getting uid.

I have give site url in facebook: http://mysite.com/

and my redirect uri is http://mysite.com/facebook/

I have added these files in my library folder.

library/fb_connect.php
library/facebook/facebook.php
library/facebook/base_facebook.php
library/facebook/fb_ca_chain_bundel.crt

Last 3 files in facebook folder are facebook SDK files.

Here is code in my fb_connect.php

<?php
include(APPPATH.'libraries/facebook/facebook.php');

 class Fb_connect {


function connect(){


$facebook = new Facebook(array(
    'appId'  => 'XXXXXXX',
    'secret' => 'XXXXXXXXXXXXXX',
    'cookie' => true,
 ));

    //Get User ID
  $user=$facebook->getUser();

  if(!$user){
   $loginUrl=$facebook->getLoginUrl(array(
    'scope'         => 'email,publish_stream,user_birthday,user_location',
    'redirect_uri'  => 'http://localhost/beta/facebook/',
    'display'=> 'popup'
    ));

    redirect($loginUrl')";
    exit();


 } else {
$user_profile = $facebook->api('/me');

 print_r($user_profile);

}
 ?>

And Here is my controller code:

function  facebook(){

  $this->load->library('fb_connect');
  $user_data=$this->fb_connect->connect();

 }

There's a few things;

1.You said "and my redirect uri is http://mysite.com/facebook/ " - but in your code:

'redirect_uri'  => 'http://localhost/beta/facebook/',

Should be:

'redirect_uri'  => 'http://mysite.com/facebook/',

2.You said "I have give site url in facebook: http://mysite.com/ " - you need to specifically set the return URL in your facebook api aswell - should be "http://mysite.com/facebook"

3.You said your redirect is "http://mysite.com/facebook/" - have you set this as a route? Because normally it should be "http://mysite.com/mycontroller/facebook/"

4.You have a typo (if this is a copy + paste):

redirect($loginUrl')";

should be

redirect($loginUrl);

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