javascript - Facebook PHP SDK: $facebook->getUser() sometimes returning 0 -


This is a very disappointing problem for me because I can not reproduce this problem and I have there's nothing to do. For some reasons, the random user of my Facebook app will return to UID 0. I can tell that this is not a specific browser issue.

How my app works here I have a home page where a user can click and open the link now. This button oauth (); triggers the js function and should open the OAuth login window. See my JS code below.

  window.fbAsyncInit = function () {FB.init ({appId: 'xxxxxxxxxxxxx', // app ID channel URL: '//channel.html' , // channel file status: true, // check login status cookie: true, // enable cookies to access server session xfbml: true // parse XFBML}); FB.Canvas.scrollTo (0,0); // iframe FB.Canvas.setAutoGrow () scrolls to the window top while loading pages; // Automatically resize the height of iframe}; // Load Asynchronous SDK (function (D) {var js, id = 'facebook-jssdk', ref = d.getElementsByTagName ['script'] [0]; if (d.getElementById (id)) {return; } Js = d.createElement ('script'); js.id = id; js.async = true; js.src = "//connect.facebook.net/en_US/all.js"; ref.parentNode.insertBefore ( Js, referee);} (document)); Function oath () {FB.login {if (response.authResponse) {console.log ('Welcome is bringing your information ....'); FBPI ('/ m', function (Feedback) {Console.log ('Good to see you,' + response.name + '.'); //window.location.reload (); // Alert ("Bing bong bing"); $. Fancybox .open ("# Entry Form", {Width: 645, Minhite: 920, Padding: 0, Scrolling: 'No', Wrap CSS: 'Enter-Form', Type: 'iframe', href: 'Upload ', Helpers: {Overlay: {Opacity: 0.95, CSS: {' background-color ':' #fff '}}} }}}};} Else {console.log ('The user has either canceled the login or is not fully authorized.');}});} // and the Oath function   

If the Oath Login is successful, then I open the Firefox box IFrame in it, which has a form IU to get the UID, the Facebook PHP code on the iframe page ...

  Lt ;? Php header ('P3P: CP = "IDC DSP Core ADM Devi Tai PSA PSD IVDi Its Our India Cnt' ');? & Gt; Php session_start (); ? & Gt; & Lt ;? Php require_once ("../ src / facebook.php"); ? & Gt; & Lt ;? Php $ config = array (); $ Config ["appId"] = 'xxxxxxxxxxxxxx'; $ Config ["secret"] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; $ Config ["fileUpload"] = Wrong; // Optional $ facebook = new Facebook ($ config); // Get a signed request to help ensure that the user is signed $ $ an administrator, confirm = $ facebook-> GetSignedRequest (); // Get the User ID $ user = $ facebook- & gt; GetUser (); // We can not or do not depend on the data that the user is logged in or not. // If we have a $ user id, it means that we know that the user is logged into Facebook, but we know that the access token is valid. If the user is logged out from Facebook then an access / token is invalid if ($ user) {try {// go ahead, to find out that you have logged in user who is certified $ user_profile = $ facebook- & gt; API ('/ m'); } Hold (FacebookApiException $ e) {error_log ($ e); $ User = null; }} // login will require login or logout url depending on user location. If ($ user) {$ logoutUrl = $ facebook- & gt; GetLogoutUrl (); } And {$ loginUrl = $ facebook-> gt; GetLoginUrl (); }? & Gt; & Lt ;? Php echo $ user // Is this user ID? & Gt;    

Most likely, you have not successfully authenticated the user in some circumstances such as There are opportunities where users will go with the PHP SDK through the initial authentication process but have failed to get the login token. In this case, FacebookApiException is thrown.

  If ($ user) {try {// go ahead, to know that you are logged into the user who is authenticated. $ User_profile = $ facebook- & gt; API ('/ m'); } Hold (FacebookApiException $ e) {error_log ($ e); $ User = null; }}   

Reference:

Comments