Take the 2-minute tour ×

I have used the facebook php sdk and I want to get the value in another file, so I give the redirection path, after login it is going on the page but $user is blank, when I am refreshing the page then I got the values. my code is like below From where the login page is calling :

$facebook = new Facebook(array('appId'  =>FB_APP_ID ,'secret' => FB_APP_SECRET));
                                                $login_url= $facebook->getLoginUrl($parameters);

and the test.php is like below

$facebook = new Facebook(array(
        'appId'  => '224402951020359' ,
        'secret' => 'd2731260cdd7425dc11eee5f265a3dee'


        $user = $facebook->getUser();

        if ($user) {
          try {
            $user_data = $facebook->api('/me');
          } catch (FacebookApiException $e) {

            $user = null;


How can I get the value in first time.

share|improve this question
add comment

1 Answer

On your test.php page, put a test for !$user and if true then redirect again to the loginurl... something like the following is what I do in my code to avoid this same problem.

    $loginUrl = $facebook->getLoginUrl(
            'scope' => 'email,publish_stream,user_birthday,user_location'

if ($user) {
  try {
    //get user basic description
    $fb_access_token = $facebook->getAccessToken();
  } catch (FacebookApiException $e) {
    error_log('Sharebox: '.$e); // d is a debug function defined at the end of this file
    $user = null;

if (!$user) {

Hope this helps.

share|improve this answer
add comment

Your Answer


