0

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"];

    ?>

In callback.php, session and header(its not redirecting to members.php) is not working, but when I open members.php manually, I am able to pring hii

Can you please suggest where I am doing wrong?

Siva
  • 3,297
  • 7
  • 29
  • 35
  • Did you find out exactly where it fails? I won't check it all but you can't send headers after sending output, and for example you do `echo "entered";` before the redirection. Remove all those `echo`s and try again. – ishegg Oct 14 '17 at 02:04
  • After I have removed echo's its redirecting now, but session is not working :-( I am not able to retrieve the values.. – Siva Oct 14 '17 at 12:26

0 Answers0