PHP 5.4.x makes a few changes to the php.ini
file which causes the Facebook PHP SDK to break, and after a few hours of searching, I’ve come across a solution that worked for me. The problem was that after authenticating an user using $facebook->getLoginUrl()
, the subsequent $facebook->getUser()
call would fail, retuning 0
.
The issue is with the PHP SDK, which relies on $_REQUEST
from the server, which has changed in PHP 5.4.0 because of default php.ini
settings having been changed.
The solution is to change the SDK’s getCode()
method (found in base_facebook.php
file to work around the issue:
Replacing:
With:
The code simply combines the $_GET
, $_POST
and $_COOKIE
arrays as with how $_REQUEST
used to work prior to PHP 5.4.0. Hope this solution works for you as it did for me.
The change should then correctly return a valid User ID when calling $facebook->getUser()
, instead of 0
. This code also works in CodeIgniter, which is where I first tested the solution.
THANK YOU very much for posting the fix.
This did not work for me and I am using laravel 4 and facebook/php-sdk from packagist, do you have another solution?
Do you get a error message or anything else that could help? What version of PHP are you using and what happens when you echo the value returned by
$facebook->getUser();
?This solution is good for the version 5.3.16 on OVH hosting
Gracias!
Hi
I use php 5.4.19 and I tried this change but not work. Any idea?
Please email me using the contact page and provide information on the issue you’re experiencing, and include snippet of your code and any errors you’re getting.
Same with laura I ve tried to use this on PHP 5.3.20 and but not work. facebook getUser keep returning 0 as the result. The CSRF state token does not match one provided. error is gone, but when it redirected to the redirect_uri which is the address is the same, it always creating different state
hello, I am in the situation mentioned, you find a solution for this problem? Thanks before hand. Sorry for the English translated by google.
Thank you for your posting,
But same with Wahya Biantara for me.
PHP 5.3.3 on CentOS.
Could you help me?
Thanks in advance.
Justin, please use the contact form to email me. Include your source code or snippet and I can try and help you out. I was able to help Laura out above.
Hi Niraj,
Thank you for your reply.
I just found solution.
It’s because of php.ini setting.
After taking out curl_exec in disable_functions directive it started to work. 🙂
Good to know Justin. Curl is a important function used by the Facebook SDK so makes sense.
Hi , I am facing the same problem as even after authentication $facebook->getUser() always returns 0. I am breaking my head from last 2 days 🙁 .I am using code igniter version : 2.1.3 and latest PHP SDK available.
Sceanrio
————
I have one controller which handles request before Facebook login as well as after facebook login . So after facebook login also $facebook->getUser() is giving the value 0 so i am not able to get the logged in user details . In App configuration i have mentioned site url as http://localhost and domain as localhost . Can this be a problem? I tried above fix mentioned by you but it dint work for me. Please help.
I got same problem, i user other domain (not localhost) but notwork either
Thank You Very Much 🙂 It really help me a lot!
I have php 5.5.9 still having sometimes this error, my fb login sometimes works sometimes not, strangely couldn’t figure out. For example if user didn’t logined in to facebook, there is facebook login screen then redirection works but it gives access token error.
If you are storing the
$session
in a PHP Session ($_SESSION
), the user will remain logged in to the app, even if they logout of Facebook itself. This is because they access_token in the$session
will work for up to an hour. You can detect if the user is really logged in by combining the PHP SDK with the JavaScript SDK.Hi, Niraj I have got little problem in Facebook Login I am using Facebook PHP SDK 4.0.X With CodeIgniter & I use same page Facebook Login but I can’t get login into app , i also get responce in url as &code= but shows me login page again , i Used Same login scheme you used but state changes everytime so can’t get in ………
Can you help me?
The state will change on every page load, but on login, the last state will be sent to Facebook and verified on successful login. Make sure you’re checking if the user is already logged in correctly, and managing the PHP SESSION manually. Use debug code (e.g.
var_dump()
orprint_r()
) to see what your code is doing, and where it could be going wrong.I have checked everything in one Url it’s work fine but in other not working at all , shows me login page even after redirecting from facebook , getSessionFromRedirect() returns NULL ………….
It sounds like your settings or app config could be wrong. Does the SDK return a Exception (try adding a
try...catch
statement around the code)? Make sure the App ID and Secret are correct for the second application and also double check the app URLs and settings. If you are using a subdomain (e.g. http://www.domain.com vs domain.com), make sure the URL is the same across the Facebook App Settings and your app (this also tends to cause issues).Can you tell me the reason behind that why getSessionFromRedirect() returns NULL , because it’s all goes wrong behind it ……………
I can’t tell you what’s wrong without looking at your complete code. You should add debug code at every stage to make sure there isn’t an error elsewhere in the code.
object(Facebook\FacebookRedirectLoginHelper)#2 (6) { [“appId”:”Facebook\FacebookRedirectLoginHelper”:private]=> string(16) “my_id” [“appSecret”:”Facebook\FacebookRedirectLoginHelper”:private]=> string(32) “my_secret” [“redirectUrl”:”Facebook\FacebookRedirectLoginHelper”:private]=> string(32) “my_redirect_url” [“sessionPrefix”:”Facebook\FacebookRedirectLoginHelper”:private]=> string(6) “FBRLH_” [“state”:protected]=> string(32) “a_32_character_string” [“checkForSessionStatus”:protected]=> bool(true) } session: NULL
This is var_dump of FacebookRedirectLoginHelper object $helper and of $session -> getSessionFromRedirect() which is returns NULL always …………
You should follow Facebook PHP SDK 4.0.x tutorial to see what you’re doing wrong. doing wrong. This article is for the older version of the PHP SDK (3.x).
Thank You Niraj , It’s Solved ……….
Thank You Very Much, Thank You Very Much ………… ………. ……….. ……….. ………..
I am using PHP 5.3.3 due to some reason i am unavailable to update php version.
So Which Facebook SDK to use with PHP 5.3.3?
http://174.46.237.233/beautypro/fb/fbconfig.php
You will need to use the older Facebook PHP SDK (v3.x).