我不确定您是如何让他们登录 facebook,但就个人而言,我使用:
if ($fbinfo['facebook_app_id']) {
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => $fbinfo['facebook_app_id'],
'secret' => $fbinfo['facebook_app_secret'],
));
// Get User ID
$user = $facebook->getUser();
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
if (!$_SESSION['extened_fb_token']) {
// ask for the extended token and get it from session ...
$facebook->setExtendedAccessToken();
$access_token = $_SESSION["fb_".FB_APP_ID."_access_token"];
// now set it into the facebook object ....
$facebook->setAccessToken($access_token);
// now our fb object will use the new token as usual ...
setCompanyFacebookExtendedToken($facebook->getAccessToken());
}
//print_r($_SESSION);
//print_r(facebookGetUserPages($user));
//echo $_SESSION['extened_fb_token'];
} else {
$loginUrl = $facebook->getLoginUrl(array('scope'=>'manage_pages,publish_stream'));
}
}
有了这个,我已经对数据库进行了查询以找出应用程序密钥是什么(因为我的数据库中有“公司”,每个人都可以为他们的用户拥有自己的 fb 应用程序)。使用他们的信息,我使用 facebook api 来确定他们是否登录。如果不是,我会生成登录 url。一旦他们登录,$user
包含他们的 facebook 用户 ID,然后可以在我自己的数据库中查询它以找出它实际上是哪个用户。在 if 语句 where $user
is true 中,我可以进行 db 调用,提取该用户的电子邮件地址并将其分配给我自己的$_SESSION['user']
变量。所以对你来说,它可能看起来像:
if (isset($_POST)) {
//Handle your post normally here
}else{
if ($fbinfo['facebook_app_id']) {
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => $fbinfo['facebook_app_id'],
'secret' => $fbinfo['facebook_app_secret'],
));
// Get User ID
$user = $facebook->getUser();
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
//This is just getting an extended token that is good for 60 days
if (!$_SESSION['extened_fb_token']) {
// ask for the extended token and get it from session ...
$facebook->setExtendedAccessToken();
$access_token = $_SESSION["fb_".FB_APP_ID."_access_token"];
// now set it into the facebook object ....
$facebook->setAccessToken($access_token);
// now our fb object will use the new token as usual ...
setCompanyFacebookExtendedToken($facebook->getAccessToken());
}
include"myconfig.php";
//The email from fb has already been inserted into my mysql users table...
$query = sprintf("SELECT * FROM users WHERE fb_id = %s",$user);
$res = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($res);
echo($row['user_name']);
echo "<b> EMAIL : </b>" . $row['user_email'];
$_SESSION['user'] = $row['user_email'];
} else {
$loginUrl = $facebook->getLoginUrl(array('scope'=>'manage_pages,publish_stream'));
}
}
}
编辑
对不起,我猜这并没有完全回答你的问题。您实际上可以重命名您自己的用户会话变量,或者一旦他们使用 facebook 登录,从 facebook 获取您需要的数据然后覆盖他们的会话变量(基本上将他们从 facebook 中注销,但您已经获得了您需要的数据脸书反正)。真的,我认为这是让他们登录 facebook 所需的全部令牌,尽管它可能会导致用户会话变量在页面加载时被覆盖,因此您需要完全删除 facebook 会话,或者只是重命名您的会话变量。
(我还是 facebook api 的新手,所以我希望这是有道理的。)