Facebook API:使用 JavaScript SDK 登录,然后使用 PHP 检查登录状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17636299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Facebook API: Login using JavaScript SDK then checking login state with PHP
提问by user1386254
I'm using Facebook's JavaScript SDK to bring up a Login Popup when the user clicks the Login button.
我正在使用 Facebook 的 JavaScript SDK 在用户单击登录按钮时显示登录弹出窗口。
The code is, as Facebook provides in the doucmentation:
正如 Facebook 在文档中提供的那样,代码如下:
$(".loginButton").click(function(){
FB.login(function(response) {
FB.api('/me', function(response) {
console.log(response.id);
//User ID shows up so I can see that the user has accepted the app.
});
});
I can also use FB.getLoginStatus()
to check that the user has indeed logged in and accepted the application.
我还可以FB.getLoginStatus()
用来检查用户是否确实登录并接受了应用程序。
However, now I would like to execute a function with PHP. As far as I understand, PHP has $user
which is assigned the UserID after a successful login.
但是,现在我想用 PHP 执行一个函数。据我了解,PHP 已$user
在成功登录后为其分配了 UserID。
The problem is after the JS Login $user
is still 0
. I'm stuck and I can't figure out why it won't assign the correct user ID to $user
问题是在 JS Login 之后$user
还是0
. 我被卡住了,我不明白为什么它不会将正确的用户 ID 分配给$user
Here's my JS:
这是我的JS:
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $AppId; ?>',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true,
channelUrl : '<?php echo $ServerPath; ?>channel.php' // custom channel
});
};
//Get Login Status
function amILoggedIn(){
var toreturn = "";
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
window.userId = response.authResponse.userID;
toreturn = "GetLoginStatus: logged in" + " " + "<?php echo $user; ?>";
} else {
toreturn = "GetLoginStatus: Logged Out";
}
console.log(toreturn);
});
};
// Load the SDK asynchronously
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
var obj = {
method: 'feed',
link: 'http://www.google.com'
};
$(".loginPopupButton").click(function(){
FB.login(function(response) {
FB.api('/me', function(response) { //user-specific stuff
console.log(response.id);
});
});
});
</script>
And here's the PHP:
这是 PHP:
<?php
include_once('fbapi/facebook.php');
include_once('fbapi/Authenticated_User.php');
include_once('config.php');
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $AppId,
'secret' => $AppSecret
));
//Facebook Authentication part
$user = $facebook->getUser();
$perms = "manage_pages";
if(isset($_GET['backlink']))
$redirectUrl = urldecode($_GET['backlink']);
else
$redirectUrl = $fullserverpath;
$cancelUrl = $ServerPath."index.php?page=loginWFb";
//AA - where to go to get FB popup.
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => $perms,
'redirect_uri' => $redirectUrl,
'cancel_uri' => $cancelUrl
)
);
//AA- Defining that a powerUSER is someone who's logged in
if(isset($user)){
$_SESSION['loggedIn'] = $user;
}
?>
回答by Ale
Login with the JS SDK stores the login data in the Facebook cookies while login with the PHP SDK stores the data in the PHP session. So if you log with the JS SDK, the PHP SDK will not be aware of that.
使用 JS SDK 登录将登录数据存储在 Facebook cookie 中,而使用 PHP SDK 登录将数据存储在 PHP 会话中。因此,如果您使用 JS SDK 登录,PHP SDK 将不会意识到这一点。
To login with the PHP SDK using the JS SDK data, you need to call your PHP page with the signed request in the query parameter:
要使用 JS SDK 数据登录 PHP SDK,您需要使用查询参数中的签名请求调用您的 PHP 页面:
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// Full redirect or ajax request according to your needs
window.location.href = 'login.php?signed_request='+response.authResponse.signedRequest;
}
});
Then $facebook->getUser()
will give you the user id in your PHP code.
然后$facebook->getUser()
将在您的 PHP 代码中为您提供用户 ID。
回答by Ale
When initializing the Facebook object in PHP, try setting the sharedSession
parameter to true
:
在 PHP 中初始化 Facebook 对象时,尝试将sharedSession
参数设置为true
:
$facebook = new Facebook(array(
'appId' => $AppId,
'secret' => $AppSecret,
'sharedSession' => true
));
回答by jakhaniya jayesh
you can find working example from here
你可以从这里找到工作示例
http://liferaypower.blogspot.com/2013/10/index.html
http://liferaypower.blogspot.com/2013/10/index.html
Sample code
示例代码
FB.init({
appId: '490986011016528', // App ID
channelUrl: 'http://localhost:8000/', // Channel File
status: true, // check login status
cookie: true, // enable cookies to allow the server to access session
xfbml: true // parse XFBML
});
FB.Event.subscribe('auth.authResponseChange', function(response)
{
if (response.status === 'connected')
{
document.getElementById("message").innerHTML += "ConnectedtoFacebook";
}
else if (response.status === 'not_authorized')
{
document.getElementById("message").innerHTML += "<br>Failed to Connect";
//FAILED
}
else
{
document.getElementById("message").innerHTML += "<br>Logged Out";
//UNKNOWN ERROR
}
});