Facebook PHP getUser() returns 0
Strange. I did not touch any of the existing CodeIgniter (PHP) codes and why suddenly Facebook sign up no longer works ? After diff-ing around here and there, only then I realized that I've upgraded the PHP version from 5.3.x to 5.4.2.
The problem was that PHP 5.4.x makes a few changes to the php.ini file and this causes the Facebook PHP SDK to break. After authenticating the user, it will just display the current page but with an extra
?code=XXXXXXX
parameter appended at the back of the URL. Further investigation shows that $facebook->getUser() function will return 0 and thus break the existing code if-statement.
Facebook PHP SDK relies on $_REQUEST from the server and because of the breaking changes in PHP 5.4.x, it no longer worked the way it supposed to be.
To fix this problem, change the Facebook SDK getCode() function in base_facebook.php file.
from
<?php
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;
}
?>
to
<?php
protected function getCode() {
$server_info = array_merge($_POST, $_GET, $_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 new code replaced $_REQUEST with a combined array of $_POST, $GET and $_COOKIE. This new changes worked and getUser() function no longer returns 0.
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+22.9k Golang : Print out struct values in string format
+7.2k Golang : Create zip/ePub file without compression(use Store algorithm)
+21.5k Golang : Use TLS version 1.2 and enforce server security configuration over client
+7.3k Golang : Detect sample rate, channels or latency with PortAudio
+8.9k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+5.5k Fix yum-complete-transaction error
+7.9k Golang : Randomize letters from a string example
+16.1k Golang : Test floating point numbers not-a-number and infinite example
+24.2k Golang : GORM read from database example
+4.5k PHP : Extract part of a string starting from the middle
+6.5k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?