php - Facebook 登录将错误的 ID 插入 mysql 数据库

标签 php mysql facebook authentication

我需要使用双重机制将 facebook 登录添加到我的网站。第一个当用户单击 fb 登录时触发,脚本检查 fbid 是否存在于我的数据库中,如果 true 设置 session 。如果 fbid 不存在,脚本会获取用户名和电子邮件等其他数据,然后将 fbid 插入数据库。问题是如果 fbid 不存在,我的代码会将错误的 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 ";

?>

最佳答案

逻辑似乎可以使用 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');

关于php - Facebook 登录将错误的 ID 插入 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20914054/

相关文章:

php - fatal error : Call to undefined method stdClass

php - 使用 PHP 在 Apache 服务器中部署 WebSockets

mysql - 与搜索相关的查询

java - Facebook 分享图片 sharedialog

php - 模态内部的不同输出(Laravel 5.4)

PHP preg_replace 符号

php - 重复值不断删除其他产品页面

mysql - JdbcTemplate - SQLWarning 忽略 : SQL state '22007' , 错误代码 '1292' ,消息 [截断的不正确的 DOUBLE 值: 'stepExecutionContext[toId]' ]

node.js - 如何使用 Facebook Graph API 发布视频

ios - 无法转换 FBSDKGraphRequestHandler 类型的值