Facebook登录php sdk会话和标头无法正常工作

时间:2017-10-14 01:56:05

标签: php facebook facebook-php-sdk

的login.php

    <?php 

    include 'config.php';

    $permissions = ['email']; // Optional permissions
    $loginUrl = $helper->getLoginUrl('https://www.example.com/callback.php', $permissions);

    echo '<a href="' . htmlspecialchars($loginUrl) . '">Log in with Facebook!</a>';

    ?>

的config.php

    <?php

    require_once "Facebook/autoload.php";

        $fb = new \Facebook\Facebook([
          'app_id' => '',
          'app_secret' => '',
          'default_graph_version' => 'v2.10'
          ]);

        $helper = $fb->getRedirectLoginHelper();


        if(isset($_GET['state']))
        {
            $helper->getPersistentDataHandler()->set('state', $_GET['state']); 
        }


    ?>

callback.php

    <?php

        session_start();

        include 'config.php';


        try {
            $accessToken = $helper->getAccessToken();
        } catch(Facebook\Exceptions\FacebookResponseException $e) {  
            echo 'Graph returned an error: ' . $e->getMessage();
            exit;
        } catch(Facebook\Exceptions\FacebookSDKException $e) {
            echo 'Facebook SDK returned an error: ' . $e->getMessage();
            exit;
        }

        if(!isset($accessToken)) {      
            header('Location: https://www.example.com/');
        }else{  

                // Logged in
                echo '<h3>Access Token7</h3>';
                var_dump($accessToken->getValue());


                if (!$accessToken->isLongLived()) {
                  try {
                    echo "entered";
                    // The OAuth 2.0 client handler helps us manage access tokens
                    $oAuth2Client = $fb->getOAuth2Client();
                    $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
                  } 
                  catch (Facebook\Exceptions\FacebookSDKException $e) 
                  {
                    echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
                    exit;
                  }
                  echo '<h3>Long-lived</h3>';
                  var_dump($accessToken->getValue());
                }           

                $response = $fb->get('/me?fields=id,name,email,first_name,last_name,picture', $accessToken->getValue());
                $user = $response->getGraphUser();      



                $_SESSION["fb_access_token"] = (string)$accessToken;
                $_SESSION["userData"] = $user;

                header('Location: https://www.example.com/members.php');
        }   



    ?>

members.php

    <?php

    session_start();

    echo "hii";

    echo $_SESSION["userData"]["name"];

    ?>

在callback.php中,会话和标题(它没有重定向到members.php)不起作用,但是当我手动打开members.php时,我能够pring hii

你能告诉我哪里做错了吗?

0 个答案:

没有答案