我的主页是index.php
。在这里,我执行用户登录,然后使用ajax请求重定向到url.php
。现在用户可以移动到各个页面。
我需要在这些页面上显示当前用户的脸书ID。我怎么能得到它?
的login.php
<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){
FB.login(function(response) {
if (response.status === "connected")
{
LodingAnimate();
FB.api('/me?fields=movies,email,name', function(mydata) {
$.post('movies_db.php',{'myd':a, name: name, email: email, myid:myid}, function(data)
{
$.ajax({
url:'url.php'
,async: true
,type : 'POST'
,cache: false
,data : 'myid=' + myid
,dataType: 'html'
,success: function(data){
$('body').html(data);
FB.XFBML.parse();
}
}
我试过这种方式:在url.php /或我想要获取会话信息的任何页面
$id= $_SESSION['fb_<appID>_user_id'];
echo "X is $id";
使用php sdk的另一个尝试:
require_once("facebook.php"); //given correct path
$config = array();
$config['appId'] = '1788xxxxxx2';
$config['secret'] = '390c04c60xxxxxxxxx68aca44';
$facebook = new Facebook($config);
$appId = $facebook->getAppId(); //gives correctly
$uid = $facebook->getUser(); //always 0
echo "$uid";
再试一次:
<?php
session_start();
print $_SESSION['fb_<myAppID>']; //prints correct
//print $_SESSION['fb_<userID>'];
print $_SESSION['fb_<myAppID>_user_id'] = '<user_id>'; //does not print anything
//echo "id :$x";
?>
没有运气。 还有其他想法吗?或者这里的错误在哪里?
答案 0 :(得分:6)
script.js
$('#login').click( function(e){
e.preventdefault;
FB.login(function(r) {
if (r.authResponse) {
var accessToken = r.authResponse.accessToken;
$.ajax({
type : "GET",
url : "set-session.php",
data : { accessToken : accessToken },
datatype : 'json',
success : function(user) {
$('p').fadeOut( function() {
$('p').html('Hey, Good to see you <a href='+ user.link +'>'+ user.first_name +'</a>!</p>');
$('p').fadeIn();
});
$('#login').fadeOut().remove();
},
error : function() {
$('p').fadeOut( function() {
$('p').html('Something went wrong :( try again');
$('p').fadeIn();
});
}
});
} else {
$('p').fadeOut( function() {
$('p').html('You didn\'t authorize the app');
$('p').fadeIn();
});
}
});
});
set-session.php
<?php
require_once 'facebook-sdk/facebook.php';
header('Content-Type: application/json');
if ( isset($_GET['accessToken']) && !empty($_GET['accessToken']) ) {
$accessToken = $_GET['accessToken'];
$facebook = new Facebook(array(
'appId' => '1419692231579671',
'secret' => 'd69245290e6c6fb1346faa32437652c5',
'cookie' => true
));
# set the access_token for $facebook for future calls
$facebook->setAccessToken($accessToken);
try {
$userData = $facebook->api('v2.0/me');
# register a new session for the user, containing their basic information, id, username, first_name, last_name, profile_picture
# and another one for the access_token.
$_SESSION['fb-user-token'] = $accessToken;
$_SESSION['fb-user'] = $userData;
echo json_encode( array('id' => $userData['id'],
'link' => $userData['link'],
'username' => $userData['uesrname'],
'first_name' => $userData['first_name'],
'last_name' => $userData['last_name'],
'gender' => $userData['gender'] ), JSON_PRETTY_PRINT);
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
header('HTTP/1.1 400');
}
index.php
<?php
if ( isset($_SESSION['fb-user']) ) {
$user = $_SESSION['fb-user'];
?>
<div>
<p>Hey, Good to see you <a href="<?php echo $user['link'] ?>"><?php echo $user['first_name']; ?></a>!</p>
</div>
<?php
} else {
?>
<div>
<p>Hey there, Login with your Facebook</p>
</div>
<a class="fsl fsl-facebook" href="#" id="login">
<span class="fa-facebook fa-icons fa-lg"></span>
<span class="sc-label">Login W/ Facebook</span>
</a>
<?php
}
?>
你可以下载&amp;在GitHub
上为此存储库做出贡献答案 1 :(得分:1)
Index.php ajax: 使用php sdk方法进行Authed,登录时单页重新加载。
<script>
function TestAjaxSession(pid){
var thisxmlhttp = "xmlhttp"+pid+"";
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
thisxmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
thisxmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
thisxmlhttp.onreadystatechange=function()
{
if (thisxmlhttp.readyState==4 && thisxmlhttp.status==200)
{
document.getElementById(pid).innerHTML=thisxmlhttp.responseText;
}
};
thisxmlhttp.open("GET","session.test.php" ,true);
thisxmlhttp.send();
};
</script>
<div id="sessiontest"><div>
<script>TestAjaxSession('sessiontest');</script>
被叫页面:“session.test.php
” 注意: 之前没有空格{{ 1}}
session_start();