我需要使用双重机制将facebook登录添加到我的网站。当用户单击fb登录时,第一个触发,脚本检查我的数据库中是否存在fbid,如果为true则设置会话。如果fbid不存在,脚本会选择其他数据,如用户名和电子邮件,然后将fbid插入到DB中。问题是如果fbid不存在,我的代码会在db中插入错误的id。这是我的代码:
<?php
require_once('lib/facebook.php');
include "db.php";
$statara='';
$config = array(
'appId' => 'xxxxxxxx',
'secret' => 'xxxxxxx',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$fb_user='';
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$fql = 'SELECT uid,name from user where uid = ' . $user_id;
$ret_obj = $facebook->api(array(
'method' => 'fql.query',
'query' => $fql,
));
// FQL queries return the results in an array, so we have
// to get the user's name from the first element in the array.
}
catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
$udj= 'Please <a href="' . $login_url . '">login. redef</a>';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, so print a link for the user to login
$login_url = $facebook->getLoginUrl();
$udj= '<a href="' . $login_url . '" id="fblogin">login. redeff</a>';
}
if ($user_id){
$sqlf=mysql_query("SELECT * FROM users where fbid='$user_id'");
$sqlf_num=mysql_num_rows($sqlf);
if ($sqlf_num > 0) { //// Users fbid exist put out his username from db
while($frow=mysql_fetch_array($sqlf)){
$username=$frow['username'];
$fb_user=$username;
}}else{ //// users fbid does not exist so insert him
$fb_user="$user_id to addd";
$sqli=mysql_query("INSERT INTO users(fbid) VALUES('$user_id')");
$stat= "<br><a href='logout.php'>logout ".$fb_user." </a><br>";
}
} else{
$stat="<br>not connected<br>";
}
echo "$udj $fb_user ";
?>
答案 0 :(得分:0)
逻辑似乎可以使用FQL获取用户详细信息。现在我看到您将用户添加到数据库中的部分
$fb_user="$user_id to addd"; // not sure what is this
$sqli=mysql_query("INSERT INTO users(`fbid`) VALUES('".$user_id."')");
$stat= "<br><a href='logout.php'>logout ".$fb_user." </a><br>";
你能试试吗
$sqli=mysql_query("INSERT INTO users(`fbid`) VALUES('".$user_id."')");
此外,您可以在执行插入之前通过回显$ user_id进行调试。
此外,您可以使用图形api调用而不是FQL来获取用户信息
$facebook->api('/me');
答案 1 :(得分:0)
尝试将表格中的fbid 列类型从 INT 更改为 BIGINT 。 这对我有用。
其他信息,我的BIGINT列类型默认长度为20。