0

I want to add Login with Facebook button to my WebPage. I logged successfully with SDK v4.0.

But I only logged within the same page. ($helper = new FacebookRedirectLoginHelper(SAME_PAGE))

Here is my code(loginFB.php). It works fine, but my question is: How can i include this PHP in an other page (defaultLogin.php). I cannot see $session var form there, it logs in (because its returning to my defaultLogin.php with ?code=FACEBOOK_AUTH_CODE), but i cannot see the session.

Can anyone help?

Thanks!

loginFB.php

<?php

session_start();

if(isset($_GET['action']) && $_GET['action'] === 'logout'){
    $session->destroySession();
    unset($_SESSION['token']);
    unset($session);
}

require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphUser.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
require_once( 'config/fbconfig.php' );

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;


// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication(APP_ID,APP_SECRET);

// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://myWeb/defaultLogin.php' );

try{
    $session = $helper->getSessionFromRedirect();
}catch(Exception $e){
    //echo "Facebook error: ". $e->getMessage();
}

if(isset($session)){
    $_SESSION['token'] = $session->getToken();
    echo "Login Successful<br>";
    $request = new FacebookRequest($session, 'GET', '/me');
    $response = $request->execute();
    $graph = $response->getGraphObject(GraphUser::className());
    $graph2 = $response->getGraphObject()->asArray();



    echo "Id: " . $graph->getId() . "<br>";
    echo "Hi " . $graph->getName() . "<br>";
    echo "Email: " . $graph2['email'] . "<br>";
    // print profile data
    echo '<pre>' . print_r( $graph2, 1 ) . '</pre>';

    echo "<img src='http://graph.facebook.com/".$graph->getId()."/picture?type=large' border=0>";

    echo "<script> checkUser('".$graph->getId()."','".$session->getToken()."','".$graph2['email']."'); </script>";

    echo '<a href="?action=logout">Logout</a>';


}
else{
    echo "<a href = " . $helper->getLoginUrl(array( 'email', 'user_friends' )) . ">Login con Facebook v1</a>";
}


if(isset($_SESSION['token'])){
    $session = new FacebookSession($_SESSION['token']);

    try{
        $session->Validate(APP_ID, APP_SECRET);
    }catch(FacebookAuthorizationException $e){
        $session = '';
    }
}
?>

defaultLogin.php

<!DOCTYPE html>
<?php session_start(); ?>
<html prefix="og: http://ogp.me/ns#" lang="es" conduit_compatibility_version="1" style="margin-top: 0px; position: relative !important;"><!--<![endif]--><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    <meta charset="utf-8">
    <?php include 'head.php';?>   

(...)

<?php include 'header.php';?>  
<?php include 'navLogin.php';?> (...)

navLogin.php

<?php include_once('loginFb.php')?>
 <!-- Navigator BAR -->
    <nav class="navbar navbar-inverse navbar-static-top" role="navigation">
        <div class="container"> (...)

1 Answer 1

0

you are missing few classes in your code . you need to add the following classes to your code before the line use Facebook\FacebookSession; and it should be working .

require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/Entities/SignedRequest.php' );

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.