我已经登录工作正常了,我可以登录,接受应用程序(第一次)然后显示用户信息,如姓名/图片/等。
当我刷新页面时,用户ID会回到0并且我必须再次登录 - 我不确定问题是什么,每次页面加载时我都必须重新启动它?我不知道,我会发布一些代码 - 它可能很难,因为它们是一堆独立的文件:
fb_login.php -
<?php
require_once("fb_login/facebook.php");
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret'=> 'APP_SECRET',
'cookie'=> 'true'
));
$userId = $facebook->getUser();
if ($userId) {
$userId = $facebook->getUser();
echo("userID is: $userId");
}
else{
header("Location: {$loginURL}");
echo("userId is: $userId");
}
?>
navbar.php -
<?php
if($userId){
try {
$user_profile = $facebook->api('/me','GET');
echo '<li><a href="#">Welcome: ' . $user_profile['name'] . '</a></li>';
echo '<li><img src="https://graph.facebook.com/' . $user_profile['username'] . '/picture"><li>';
echo '<li class="divider-vertical"></li>';
} catch (FacebookApiException $e) {
$login_url = $facebook->getLoginUrl();
echo '<li>Please <a href="' . $login_url . '">login.</a></li>';
error_log($e->getType());
error_log($e->getMessage());
}
}
else{
echo '<li><a href="newUser.php">Sign Up</a></li>
<li class="divider-vertical"></li>';
}
?>
<?php
if ($userId) {
// echo("userID is: $userId");
// $params = array( 'next' => 'http://localhost/bcbooks-repo/index_new.php' );
$logoutUrl = $facebook->getLogoutUrl(); // $params is optional.
echo '<li><a href="' . $logoutUrl . '">Log Out</a></li>';
$facebook->destroySession();
}
else{?>
<?php
$userId = $facebook->getUser();
$accessToken = $facebook->getAccessToken();
$params = array(
'scope' => 'read_stream, friends_likes',
'redirect_uri' => 'http://localhost/bcbooks-repo/index_new.php'
);
$loginUrl = $facebook->getLoginUrl($params);
echo '<a href="' . $loginUrl . '" class="btn btn-primary btn-block" type="button" id="sign-in-facebook">Sign In with Facebook</a>';
?>
<?php } ?>
index.php -
<?php
require_once 'inc/FB_login.php';
require_once 'inc/navbar.php'; ?>
已登录 -
页面刷新后
控制台结果
答案 0 :(得分:2)
在Fabio建议的基础上尝试使用此代码(显然将YOUR_APP_ID替换为您的实际应用ID)
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: 'YOUR_APP_ID',
status: true,
cookie: true,
xfbml: true,
channelUrl:'/channel.html',
frictionlessRequests: true,
useCachedDialogs: true,
oauth: true
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
};
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
答案 1 :(得分:1)
虽然您正在使用cookie选项TRUE加载facebook PHP SDK,但服务器端无法猜测用户是否仍在您的网站上登录,或者他是否更改为其他帐户等。
如果要在所有页面中保留usar,则必须将javascript SDK与PHP SDK结合使用。要做到这一点,您只需要在每个页面中加载javascript SDK,将其放在需要与<body>
标记旁边的Facebook交互的每个页面上:
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR_APP_ID', // App ID
channelUrl : 'URL TO YOUR CHANNEL FILE', // Channel File Optional
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Additional initialization code here
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
答案 2 :(得分:0)
尝试在cookie中声明userID,如下所示:
$user = $facebook->getUser();
$expire=time()+60*60*24*30;
setcookie("user", $user, $expire);
对我而言,它就像一个魅力;)