簡體   English   中英

頁面刷新后 Facebook PHP-SDK 似乎丟失了用戶 ID

[英]Facebook PHP-SDK seems to lose userID after page refresh

我已經登錄工作正常,我可以登錄,接受應用程序(第一次)然后顯示用戶信息,如姓名/圖片/等。

然而,當我刷新頁面時,userid 返回到 0,我必須再次登錄 - 我不確定問題是什么,每次頁面加載時我都必須重新啟動它或其他什么? 我不知道,我會發布一些代碼 - 這可能很難,因為它們是一堆單獨的文件:

fb_login.php -

<?php
    require_once("fb_login/facebook.php");
    $facebook = new Facebook(array(
      'appId' => 'APP_ID',
      'secret'=> 'APP_SECRET',
      'cookie'=> 'true'
    ));
    $userId = $facebook->getUser();

    if ($userId) {
        $userId = $facebook->getUser();
         echo("userID is: $userId");
}
    else{
        header("Location: {$loginURL}");
         echo("userId is: $userId");
    }


?>

導航欄.php -

<?php
              if($userId){
                 try {
                  $user_profile = $facebook->api('/me','GET');
                  echo '<li><a href="#">Welcome: ' . $user_profile['name'] . '</a></li>';
                  echo '<li><img src="https://graph.facebook.com/' . $user_profile['username'] . '/picture"><li>';
                  echo '<li class="divider-vertical"></li>';
                 } catch (FacebookApiException $e) {
                    $login_url = $facebook->getLoginUrl(); 
                    echo '<li>Please <a href="' . $login_url . '">login.</a></li>';
                    error_log($e->getType());
                    error_log($e->getMessage());
                 }
              }
              else{
                  echo '<li><a href="newUser.php">Sign Up</a></li>
                      <li class="divider-vertical"></li>';
              }
            ?>
<?php
                  if ($userId) { 
                    // echo("userID is: $userId");
                    // $params = array( 'next' => 'http://localhost/bcbooks-repo/index_new.php' );

                    $logoutUrl = $facebook->getLogoutUrl(); // $params is optional. 
                    echo '<li><a href="' . $logoutUrl . '">Log Out</a></li>';
                    $facebook->destroySession();
                  }
                  else{?>
<?php
                $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);
                echo '<a href="' . $loginUrl . '" class="btn btn-primary btn-block" type="button" id="sign-in-facebook">Sign In with Facebook</a>';
                                        ?>
<?php } ?>

index.php -

<?php
  require_once 'inc/FB_login.php';
require_once 'inc/navbar.php'; ?>

登錄 -登錄

頁面刷新后刷新后

控制台結果控制台結果

在 Fabio 建議嘗試使用此代碼的基礎上構建(顯然將 YOUR_APP_ID 替換為您的實際應用程序 ID)

     <div id="fb-root"></div>
  <script>     

  window.fbAsyncInit = function() {
    FB.init({
      appId: 'YOUR_APP_ID', 
      status: true,
         cookie: true,
         xfbml: true,
       channelUrl:'/channel.html',
      frictionlessRequests: true,
      useCachedDialogs: true,
      oauth: true
    });
    FB.Event.subscribe('auth.login', function(response) {
      window.location.reload();
    });
    FB.Event.subscribe('auth.logout', function(response) {
      window.location.reload();
    });
  };
(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, ref);
     }(document));
      </script>

盡管您使用 cookie 選項 TRUE 加載 facebook PHP SDK,但服務器端無法猜測用戶是否仍在您的網站上登錄,或者他是否已更改為其他帳戶等。

如果您想在所有頁面上保留 usar,您必須將 javascript SDK 與 PHP SDK 結合使用。 為此,您只需在您擁有的每個頁面中加載 javascript SDK,將其放在每個需要與 facebook 交互的頁面的<body>標簽旁邊:

<body>
    <div id="fb-root"></div>
        <script>
          window.fbAsyncInit = function() {
            FB.init({
              appId      : 'YOUR_APP_ID', // App ID
              channelUrl : 'URL TO YOUR CHANNEL FILE', // Channel File Optional
              status     : true, // check login status
              cookie     : true, // enable cookies to allow the server to access the session
              xfbml      : true  // parse XFBML
            });

            // Additional initialization code here

            };


          // Load the SDK Asynchronously
          (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, ref);
           }(document));
    </script>

嘗試在 cookie 中聲明 userID,就像這樣:

$user = $facebook->getUser();
$expire=time()+60*60*24*30;
setcookie("user", $user, $expire);

對我來說,它就像一個魅力;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM