问题
I'm using Facebook's JavaScript SDK to bring up a Login Popup when the user clicks the Login button.
The code is, as Facebook provides in the doucmentation:
$(".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.
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.
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
Here's my 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
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;
}
?>
回答1:
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.
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:
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.
回答2:
When initializing the Facebook object in PHP, try setting the sharedSession parameter to true:
$facebook = new Facebook(array(
'appId' => $AppId,
'secret' => $AppSecret,
'sharedSession' => true
));
回答3:
you can find working example from here
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
}
});
来源:https://stackoverflow.com/questions/17636299/facebook-api-login-using-javascript-sdk-then-checking-login-state-with-php