我想要做的是根据用户来到我的页面,
- 检查脸书状态。如果已登录并已使用我的应用程序授权,则重定向到新页面,即
url.php
。 - 如果未登录且已获得授权,则显示
index.php
带有connect facebook
按钮的页面 - 如果我的应用未获得授权,请让他这样做。还要求记住密码,以便下次登录时,应重定向到页面
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";
它隐藏按钮