When using CodeIgniter + Facebook PHP SDK: getUser() always returns 0 When using CodeIgniter + Facebook PHP SDK: getUser() always returns 0 codeigniter codeigniter

When using CodeIgniter + Facebook PHP SDK: getUser() always returns 0


The getCode() method in base_facebook.php uses the $_REQUEST global to store data. PHP 5.3.0 and greater uses the "request_order" param in php.ini, and by default $_REQUEST does not contain Cookie variables.

Per php.net (http://php.net/manual/en/ini.core.php#ini.request-order):

"This directive describes the order in which PHP registers GET, POST and Cookie variables into the _REQUEST array. Registration is done from left to right, newer values override older values.

If this directive is not set, variables_order is used for $_REQUEST contents.

Note that the default distribution php.ini files does not contain the 'C' for cookies, due to security concerns."

So it looks like your options are to modify the getCode() method like Max Power did above, or update your php.ini and add the "C" value to the request_order setting.


I managed to solve my problem. The questions linked to by Qweick and Stéphane Bruckert had the solution. The problem lies in the getCode() function of the base_facebook.php file.

The getCode() function needs to be modified. The modifications I used are listed below.

Existing non-working code:

protected function getCode() {    if (isset($_REQUEST['code'])) {        if ($this->state !== null &&                isset($_REQUEST['state']) &&                $this->state === $_REQUEST['state']) {            // CSRF state has done its job, so clear it            $this->state = null;            $this->clearPersistentData('state');            return $_REQUEST['code'];        } else {            self::errorLog('CSRF state token does not match one provided.');            return false;        }    }    return false;}

Modified working code:

protected function getCode() {    $server_info = array_merge($_GET, $_POST, $_COOKIE);    if (isset($server_info['code'])) {        if ($this->state !== null &&                isset($server_info['state']) &&                $this->state === $server_info['state']) {            // CSRF state has done its job, so clear it            $this->state = null;            $this->clearPersistentData('state');            return $server_info['code'];        } else {            self::errorLog('CSRF state token does not match one provided.');            return false;        }    }    return false;}

The getUser() call now returns a valid user Id and the Facebook API calls now return valid data.

Thanks to everyone that helped point me in the right direction!