gpt4 book ai didi

php - Facebook PHP-SDK 似乎在页面刷新后丢失了用户 ID

转载 作者:行者123 更新时间:2023-12-01 22:53:15 25 4
gpt4 key购买 nike

我的登录似乎工作正常,我可以登录,接受应用程序(第一次),然后显示用户信息,如姓名/图片等。

但是,当我刷新页面时,userid 返回到 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");
}


?>

导航栏.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'; ?>

登录 - Logged in

页面刷新后After refresh

控制台结果 Console results

最佳答案

在 Fabio 建议的基础上尝试使用此代码(显然用您的实际应用 ID 替换 YOUR_APP_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>

关于php - Facebook PHP-SDK 似乎在页面刷新后丢失了用户 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15621586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com