2013-09-23 20 views
0

我想要做的就是按照用戶來我的網頁上,執行不同的動作按照Facebook的登錄狀態

  1. 檢查Facebook狀態。如果已登錄並且已經通過我的應用程序授權,則會重定向到新頁面,即url.php
  2. 如果沒有記錄,並已經授權,則與connect facebook按鈕
  3. 顯示index.php頁面如果不是我的應用授權,則要求他做。還要求記住密碼,以便下次登錄時,應該重定向到頁面url.php

我試過FB.getLoginStatus。當我下面執行簡單的代碼,它工作正常:

FB.getLoginStatus(function(response) { 
    if (response.status === 'connected') { 
    var uid = response.authResponse.userID; 
    var accessToken = response.authResponse.accessToken; 
    } else if (response.status === 'not_authorized') { 
    // but has not authenticated your app 
    } else { 
    // the user isn't logged in to Facebook. 
    } 
}); 

當我試圖做同樣具有以下實際的代碼就永遠不會成功,一堆試後。 實際上發生了什麼,the content on the page like按鈕and rest it always get loaded even user is logged in or not. 請給我一些暗示此代碼放置適當的條件:

mycode的:

<?php 
session_start(); 
$appId = '178862'; 
$appSecret = 'xxxxxxxxxxxxxxxxxxxxxxxx'; // Facebook App Secret 
$return_url = 'dev01.dev/FB/facebook-php-sdk/examples/'; //path to script folder 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head> 
<script type="text/javascript" src="https://www.google.com/jsapi"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"> 
</script> 
    <title>ThenWat</title> 
    <script> 
function AjaxResponse() 
{ 
    //alert("2"); 
    var mydata;   
    CallAfterLogin(mydata, function(send) { 
       }); 
    } 
function LodingAnimate() //Show loading Image 
{ 
    //alert("animate4");  
    // $("#LoginButton").hide(); //hide login button once user authorize the application 
} 

    </script> 
</head> 
<body style="height: 560px"> 
<?php 
if(!isset($_SESSION['logged_in'])) 
{ 
?> 
    <div style="z-index: 1; left: 538px; top: 290px; position: absolute;"> 
    <input class="uibutton confirm large" type="submit" value="Connect with facebook" onclick="AjaxResponse()" > 
    </div> 
<?php 
} 
?> 


<div id="fb-root"></div> 
<script type="text/javascript"> 
window.fbAsyncInit = function() { 
FB.init({ 
    appId: '<?php echo $appId; ?>', 
    cookie: true, 
    xfbml: true, 
// channelUrl: '<?php echo $return_url; ?>channel.php', 
    oauth: true});}; 
(function() { 
     //alert("1"); 
    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);}()); 
function CallAfterLogin(data,callback){ 
     //alert("CallafterLogin3"); 
     FB.login(function(response) { //--- 
    x: FB.getLoginStatus(function(response) { 
     { 
      LodingAnimate(); 
      FB.api('/me?fields=movies,email', function(mydata) { 
      console.log(mydata); 
       if(mydata.email == null) 
       { 
       alert("You must allow us to access your email id!"); 
       ResetAnimate(); 
       }   
       else 
     { 
      //alert("json-5"); 
      var json = JSON.stringify(mydata.movies.data); 
     // alert(mydata.movies); 
      var a = JSON.parse(json);  
      $.post('movies_db.php',{'myd':a}, function(data) 
      { 
       $.ajax({ 
      url:'url.php' 
      ,async:  true 
      ,cache:  false 
      ,dataType: 'html' 
      ,success: function(data){ 
       $('body').html(data); 
        } 
     });   
      }); 
     } 

       }); //-- 
     } //if 
      }); //--- 
      } //main function 
</script> 
</body> 
</html> 

當我把AjaxResponse()在onload事件,然後它不符合x進入

FB.getLoginStatus(function(response) {如果我把以前document.getElementById("b1").style.display="none";它隱藏按鈕

回答

0
1. Perform user App authentication. 
2. Store user facebook profile details in database 
3. If user has already authenticated your app, and logged into his facebook a/c the redirect to next page 
4. If user is not logged in, SDK will ask to perform facebook login 

源代碼

FB登錄 http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js「>
.middle { background-color:Yellow;在GitHub }

 .left{ 
      background-color:Green; 
     } 
     .url{ 
      box-sizing: border-box; 
      display: block; 
     } 
     .url:hover { 
      box-shadow: 2px 2px 5px rgba(0,0,0,.2); 
     } 
     .header{ 
      background-color: #61B329; 
      color: #FFF; 
      margin-top: 0px !important; 
      margin-bottom: 20px; 
      padding-bottom: 9px; 
     } 
     .page-header-text { 
      padding-left: 15px; 
      padding-top: 20px; 
      padding-bottom: 10px; 
      margin: 0px; 
     } 


     html, body { margin: 0; padding: 0; border: 0 } 
    </style> 
    <script> 
     $(document).ready(function() 
     { 
      console.log("ready!"); 
      //alert("Welcome"); 
     }); 
    </script> 
</head> 

<body> 
    <div class="page-header header"> 
     <h1 class="page-header-text">login</h1> 
    </div>  
     <table border = "0" width = "100%"> 
      <tr> 
       <div class = "middle"> 
        <td style = "width:40%"> 
        <input type = "button" id = "loginButton" class = "btn btn-primary" onclick = "authUser();" value = "Login | Facebok" style = "display:none; left:500px; margin-top:200px; position:relative"/> 
        </td>    
       </div> 
      </tr>    
     </table> 
    <div id = "fb-root"></div> 
    <script type = "text/javascript"> 
     var userid; 
     FB.init({ 
     //appId: '', 
     appId: 'app id', 
     xfbml: true, 
     status: true, 
     cookie: true, 
     }); 
     FB.getLoginStatus(checkLoginStatus); 
     function authUser() 
     { 
      FB.login(checkLoginStatus, {scope:'email'}); 
     } 
     function checkLoginStatus(response) 
     {    
      if(response && response.status == 'connected') 
      { 
      FB.api('/me?fields = movies,email,name,gender,locale,location,link', function(mydata) 
      { 
       console.log(mydata.email); 
       console.log(mydata.id); 
       userid = mydata.id; 
       var name = mydata.name; 
       gender = mydata.gender; 
       locale = mydata.locale; 
       city = mydata.location; 
       link = mydata.link; 
       //alert(name); 
       var email = mydata.email; 
       //var json = JSON.stringify(mydata.movies.data); 
       //var a = JSON.parse(json); 
       var picture = "https://graph.facebook.com/"+userid+"/picture?type = small"; 
       // alert(picture); 
       $.post('user_record.php',{ name: name, email: email, userid:userid, picture:picture, gender: gender, locale: locale, city: city, link: link}, function(data) 
       { 
        var $form = $("<form id = 'form1' method = 'post' action = 'start.php'></form>"); 
        $form.append('<input type = "hidden" name = "userid" value = "'+userid+'" />'); 
        $('body').append($form); 
        window.form1.submit(); 
       }); 
      }); 

      console.log('Access Token: ' + response.authResponse.accessToken); 
      } 
      else 
      { 
       document.getElementById('loginButton').style.display = 'block'; 
      } 
     } 
    </script> 
</body> 

源代碼:https://github.com/karimkhanp/facebookLoginSDK