Web Niraj
  • Facebook
  • Flickr
  • Github
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • Facebook PHP SDK: Fixing getUser() on PHP 5.4.x

Facebook PHP SDK: Fixing getUser() on PHP 5.4.x

29

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.

Bug, Facebook, PHP, SDK

29 comments on “Facebook PHP SDK: Fixing getUser() on PHP 5.4.x”

  1. SanityRestored! says:
    March 14, 2013 at 3:00 PM

    THANK YOU very much for posting the fix.

    Reply
  2. juan says:
    May 23, 2013 at 8:00 PM

    This did not work for me and I am using laravel 4 and facebook/php-sdk from packagist, do you have another solution?

    Reply
    • Niraj Shah says:
      May 23, 2013 at 9:41 PM

      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();?

      Reply
  3. Francesco says:
    July 9, 2013 at 7:53 AM

    This solution is good for the version 5.3.16 on OVH hosting

    Reply
  4. Alan says:
    October 2, 2013 at 8:30 PM

    Gracias!

    Reply
  5. laura says:
    October 8, 2013 at 4:53 PM

    Hi
    I use php 5.4.19 and I tried this change but not work. Any idea?

    Reply
    • Niraj Shah says:
      October 8, 2013 at 7:59 PM

      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.

      Reply
  6. Wahya Biantara says:
    October 11, 2013 at 7:38 PM

    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

    Reply
    • David says:
      December 5, 2013 at 6:26 PM

      hello, I am in the situation mentioned, you find a solution for this problem? Thanks before hand. Sorry for the English translated by google.

      Reply
  7. Justin says:
    November 7, 2013 at 6:47 PM

    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.

    Reply
    • Niraj Shah says:
      November 7, 2013 at 6:50 PM

      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.

      Reply
      • Justin says:
        November 8, 2013 at 3:32 PM

        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. 🙂

      • Niraj Shah says:
        November 9, 2013 at 11:12 AM

        Good to know Justin. Curl is a important function used by the Facebook SDK so makes sense.

  8. anon says:
    January 2, 2014 at 9:42 PM

    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.

    Reply
  9. danhkiem says:
    January 6, 2014 at 1:45 PM

    I got same problem, i user other domain (not localhost) but notwork either

    Reply
  10. Stephanus says:
    April 1, 2014 at 4:52 AM

    Thank You Very Much 🙂 It really help me a lot!

    Reply
  11. Borsa says:
    June 19, 2014 at 6:47 PM

    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.

    Reply
    • Niraj Shah says:
      June 20, 2014 at 10:54 AM

      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.

      Reply
  12. Sonali says:
    January 22, 2015 at 6:17 AM

    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?

    Reply
    • Niraj Shah says:
      January 23, 2015 at 9:19 AM

      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() or print_r()) to see what your code is doing, and where it could be going wrong.

      Reply
  13. Sonali says:
    January 23, 2015 at 10:01 AM

    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 ………….

    Reply
    • Niraj Shah says:
      January 23, 2015 at 10:07 AM

      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).

      Reply
  14. Sonali says:
    January 23, 2015 at 10:14 AM

    Can you tell me the reason behind that why getSessionFromRedirect() returns NULL , because it’s all goes wrong behind it ……………

    Reply
    • Niraj Shah says:
      January 25, 2015 at 3:26 PM

      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.

      Reply
  15. Sonali says:
    January 23, 2015 at 10:33 AM

    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 …………

    Reply
    • Niraj Shah says:
      January 25, 2015 at 3:25 PM

      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).

      Reply
      • Sonali says:
        January 25, 2015 at 4:35 PM

        Thank You Niraj , It’s Solved ……….

        Thank You Very Much, Thank You Very Much ………… ………. ……….. ……….. ………..

  16. nkkumar947 says:
    June 19, 2015 at 11:43 AM

    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

    Reply
    • Niraj Shah says:
      June 22, 2015 at 9:16 AM

      You will need to use the older Facebook PHP SDK (v3.x).

      Reply

Leave a Reply to juanCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Firewall Flash Gadget Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux NodeJS Parse PDF PHP Plugin Portfolio PS4 Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2025 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy