0
votes

First, I downloaded the Facebook PHP SDK and applied the first answer in here: Using Facebook PHP-SDK 3.x to register/login user with Codeigniter 2.1.0

In my controller:

  1. I try to get the user: $this->facebook->getUser(); .. If it's 0, I call another function $this->facebook_login(); - else I save some data in the database like name for example.

  2. In facebook_login(), a Login Url is generated: $this->facebook->getLoginUrl() - and is passed to the view, which displays it.

  3. So far I click on the login url and it redirects to Facebook, asks for approval and authentication and so on - then redirects me back to the page in "1".

  4. The problem is, getUser is returning zero. I printed the $SESSION variable and found out that only fb{appId}_state is being set.

I tried creating another php file (login.php) that is not in the CI folder with the following code:

<?php

define('BASEPATH', '');
require_once('application/libraries/facebook.php');

$fb = new Facebook(array('appId'=>'...', 'secret'=>'...'));

$user = $fb->getUser();

if($user == 0){
    $login = $fb->getLoginUrl();
    echo '<a href="'.$login.'">Login</a>';
} else {
    print_r($fb->api('/me'));
}

?>

If I visit login.php then visit the page in "1" above - getUser returns the user id and $_SESSION contains the user details.Therefore, I guess the problem is with CodeIgniter but I am not sure how to solve it.

I searched and tried several solutions but that's the best I could do. If possible, I don't want to use the JS SDK.

Thank you

1
In the constructor found in the link at the beginning of the question, add this before loading the facebook library: parse_str( $_SERVER['QUERY_STRING'], $_REQUEST ); - GalalAly
Is the cookie_domain variable set properly to your domain in application/config/config.php? - Yan Berk
Yes. I figured out the solution and posted it as a comment. Thank you :) - GalalAly

1 Answers

0
votes

I found a solution and wrote a blog post about it here.