Facebook PHP SDK - 检查用户ID

时间:2013-03-25 15:17:13

标签: php facebook facebook-php-sdk

这可能很简单,但我只是没有得到它。我正在检查$ userID,如果它存在显示注销网址,否则显示登录网址,但它总是绕过对userID的条件检查,即使我打印出来并且它存在!我刚刚碰到我的脑袋一段时间,我认为不同的观点可能会有所帮助......

<?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>';
    }


?>

1 个答案:

答案 0 :(得分:0)

基于我上面的评论

   $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();
   }

应该是

   $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();
   }