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:
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.
In facebook_login(), a Login Url is generated: $this->facebook->getLoginUrl() - and is passed to the view, which displays it.
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".
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
cookie_domainvariable set properly to your domain in application/config/config.php? - Yan Berk