0

This might be something simple, but I'm just not getting it. I'm doing a check for $userID, and if it exists show a logout url, otherwise show the login url, but it's always bypassing the conditional check for userID, even though I'm printing it out and it exists! I've just been banging my head against it for a while and I think a different perspective might help...

<?php
    require_once("fb_login/facebook.php");
    $facebook = new Facebook(array(
      'appId' => 'MYAPPID',
      'secret'=> 'MYSECRETID'
    ));
    $userId = $facebook->getUser();
    if ($userID) {
        echo("userID is: $userId");
        // $params = array( 'next' => 'http://localhost/bcbooks-repo/index_new.php' );

        $logoutUrl = $facebook->getLogoutUrl(); // $params is optional. 
        echo '<a href="' . $logoutUrl . '">logout</a>';
        $facebook->destroySession();
    }
    else{
        header("Location: {$loginURL}");
        $userId = $facebook->getUser();
        $accessToken = $facebook->getAccessToken();
        $params = array(
          'scope' => 'read_stream, friends_likes',
          'redirect_uri' => 'http://localhost/bcbooks-repo/index_new.php'
        );

        $loginUrl = $facebook->getLoginUrl($params);
        print_r($_SESSION);
        echo("userID is: $userId");
        echo '<a href="' . $loginUrl . '">login</a>';
    }


?>
Kevin
  • 565
  • 12
  • 25
  • Your variables have different names $userId != $userID lose the capital D in the if statement and make it lowercase as $userID will always be empty/null! – TommyBs Mar 25 '13 at 15:18

1 Answers1

0

based on my comment above

   $userId = $facebook->getUser();
   if ($userID) { 
    echo("userID is: $userId");
    // $params = array( 'next' => 'http://localhost/bcbooks-repo/index_new.php' );

    $logoutUrl = $facebook->getLogoutUrl(); // $params is optional. 
    echo '<a href="' . $logoutUrl . '">logout</a>';
    $facebook->destroySession();
   }

should be

   $userId = $facebook->getUser();
   if ($userId) { // <- notice lowercase d
    echo("userID is: $userId");
    // $params = array( 'next' => 'http://localhost/bcbooks-repo/index_new.php' );

    $logoutUrl = $facebook->getLogoutUrl(); // $params is optional. 
    echo '<a href="' . $logoutUrl . '">logout</a>';
    $facebook->destroySession();
   }
TommyBs
  • 9,354
  • 4
  • 34
  • 65
  • Haha don't worry,we've all been there – TommyBs Mar 25 '13 at 15:25
  • Hey Tommy, I'm having trouble with another Facebook PHP SDK (might be a bit tougher!) - think you can help me with that? http://stackoverflow.com/questions/15621586/facebook-php-sdk-seems-to-lose-userid-after-page-refresh – Kevin Mar 25 '13 at 20:07
  • HI, I've replied to your question. I also made an edit to remove your app_id and app_secret from your code in the question. You don't want to post those. – TommyBs Mar 25 '13 at 20:56
  • Thanks. Good point. I'm in class now unfortunately, but I'll try that out when I get home. You understand what the problem is though right? It's almost like I'm starting from scratch each time the page loads. Do you think it might have to do with it running off of localhost? – Kevin Mar 25 '13 at 20:59
  • localhost might be an issue. But using the js-sdk in conjunction with the php-sdk should work as I suggest in the answer. I think the issue with what Fabio suggested was that he wasn't subscribing to the "login" event so I don't think it was firing correctly. Try out my revised code and see if it works – TommyBs Mar 25 '13 at 21:01