0

我想要做的是根据用户来到我的页面,

  1. 检查脸书状态。如果已登录并已使用我的应用程序授权,则重定向到新页面,即url.php
  2. 如果未登录且已获得授权,则显示index.php带有connect facebook按钮的页面
  3. 如果我的应用未获得授权,请让他这样做。还要求记住密码,以便下次登录时,应重定向到页面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. 请给我一些提示,让这段代码适当地放置条件:

我的代码:

<?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";它隐藏按钮

4

1 回答 1

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; }

        .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> 

GIthub 上的源代码:https ://github.com/karimkhanp/facebookLoginSDK

于 2014-02-13T06:33:21.323 回答