2014-02-20 493 views
3

我正在做一個登錄和註銷與facebook帳戶信息的網站。對於那些我已經作出了這樣的代碼facebook登錄並從網站註銷?

對於我的index.html的代碼是這樣的

<div id="fb-root"></div> 
<script type="text/javascript"> 
//<![CDATA[ 
window.fbAsyncInit = function() { 
    FB.init({ 
    appId  : 'XXXXXXXXXXXXXXXX', // App ID 
    channelURL : '', // Channel File, not required so leave empty 
    status  : true, // check login status 
    cookie  : true, // enable cookies to allow the server to access the session 
    oauth  : true, // enable OAuth 2.0 
    xfbml  : false // parse XFBML 
    }); 
}; 
// logs the user in the application and facebook 
function login(){ 
FB.getLoginStatus(function(r){ 
    if(r.status === 'connected'){ 
      window.location.href = 'fbconnect.php'; 
    }else{ 
     FB.login(function(response) { 
       if(response.authResponse) { 
       //if (response.perms) 
        window.location.href = 'fbconnect.php'; 
      } else { 
       // user is not logged in 
      } 
    },{scope:'email'}); // which data to access from user profile 
} 
}); 
} 

// Load the SDK Asynchronously 
(function() { 
    var e = document.createElement('script'); e.async = true; 
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; 
    document.getElementById('fb-root').appendChild(e); 
}()); 

//]]> 
</script> 
<a href='#' onclick='login();'>Facebook Login</a> 

和fbconnect.php我有我的代碼看起來像這樣

<?php 
    require 'src/facebook.php'; 
    $facebook = new Facebook(array(
      'appId' => 'XXXXXXXXXXXXXXXX', 
      'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', 
      'cookie' => true, 
    )); 
    $user = $facebook->getUser(); 
    $loginUrl = $facebook->getLoginUrl(); 
    //echo "<a href='$loginUrl'>login</a>"; 

    $logoutUrl = $facebook->getLogoutUrl(); 
    //echo $loginUrl; 
    if($user){ 
     $_SESSION['user_info'] = $user; 
     $_SESSION['user_pro']= $facebook->api('/me'); 
     echo $_SESSION['user_pro']['first_name']; 
     echo $_SESSION['user_pro']['last_name']; 
     echo '<pre>'; 
     //print_r($_SESSION); 
     echo '</pre>'; 
    } 
    else{ 
     echo 'not logged in '; 
    } 
    echo "<a href='logout.php'>log out </a>" 
?> 

正如我也希望註銷系統,以便用戶也可以從網站註銷我做了這樣的logout.php代碼

<?php 
require 'src/facebook.php'; //including the facebook php sdk 
$token = $facebook->getAccessToken(); 
$url = 'https://www.facebook.com/logout.php?next=' . YOUR_SITE_URL . 
    '&access_token='.$token; 
session_destroy(); 
header('Location: '.$url); 
?> 

但是這裏顯示的錯誤類似於

Notice: Undefined variable: facebook in logout.php on line 3 Fatal error: Call to a member function getAccessToken() on a non-object in logout.php on line 3. 

這裏它很容易登錄,但在註銷時顯示錯誤。那麼有人可以告訴我如何解決這個問題?我真的是新手到Facebook應用程序。所以任何幫助和建議都會非常可觀。由於

回答

0

嘗試像

require 'src/facebook.php'; 
$facebook = new Facebook(); //including the facebook php sdk 
$token = $facebook->getAccessToken(); 

哪裏$facebook是爲Facebook class.And實例按你的意見,如果你想從Facebook註銷還那麼你就需要在像

你登出功能破壞會議
$facebook->destroySession(); 
+0

感謝您的答覆,但你能告訴我如何從站點註銷完全從Facebook註銷。在我的情況下,它只是從網站註銷,而不是從Facebook .. – NewUser

+0

看到我的編輯一次@NewUser – Gautam3164

+0

是的我已經使用相同的你告訴,但它不工作。我的logout.php代碼是這樣的 '<?php require'src/facebook.php'; //包括facebook php sdk $ facebook = new Facebook(); $ token = $ facebook-> getAccessToken(); $ url ='index.html'; $ facebook-> destroySession(); session_destroy(); header('Location:'。$ url); ?> – NewUser