Skip to main content

facebook iframe app; php sdk getUser() returns valid id on page one but not for any other page



I have a facebook iframe app which correctly logs in and authorizes the app, but getUser() only works on the first page. As soon as a user clicks a link to a new page within the iframe, getUser() returns 0.





What's strange is that this same code works for another app... I do all the clicking I want and getUser() returns a valid ID.





The app that doesn't work: https://apps.facebook.com/celestial_glory/





The one that does (same codebase): https://apps.facebook.com/uprisingstlouis/





Here's the code I am using:







require_once ('fb/facebook.php');



// snip... set $app_id, $secret, and $canvas_page



// first, try normal facebook getUser(). If that works, awesome.



$facebook = new Facebook(array(

'appId' => $app_id,

'secret' => $secret,

));



$signed_request = $_REQUEST['signed_request'];



// Get User ID

$user = $facebook->getUser();

if ($user != '0') return 'fb=' . $user; // works once



// getUser() didn't work. Try oAuth. Maybe user needs to log in or

// authorize the game?



$auth_url = 'http://www.facebook.com/dialog/oauth?client_id='

. $app_id . '&redirect_uri=' . urlencode($canvas_page);



list($encoded_sig, $payload) = explode('.', $signed_request, 2);



$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);



if (empty($data["user_id"])) {

echo '<a target="_top" href="' . $auth_url . '">Login to Facebook</a>';

exit;

// normally we would auto-redirect, but with a uid of 0, this just auto-redirects

// echo("<script> top.location.href='" . $auth_url . "'</script>");

} else {

return 'fb=' . $data['user_id'];

}







any ideas? I have triple-checked app ids and secrets and canvas pages. If those were wrong, I expect no page, not even the first, would work.


Comments

  1. Change Facebook PHP-SDK initialization to:

    $facebook = new Facebook(array(
    'appId' => $app_id,
    'secret' => $secret,
    'cookie' => true // this!
    ));


    getUser works on the first page because it can get the user from signed_request (POST'ed by Facebook to your canvas page URL). Thus you need some way to track your user once he starts navigation deeper within your application. You could pass signed_request somehow all by yourself or simply enable built-in PHP-SDK cookie support as suggested above.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex