douzhong5902 2014-01-03 22:41
浏览 41
已采纳

Facebook登录将错误的id插入mysql数据库

I need to add facebook login to my website with double mechanism. First one fires when user click fb login, script checks whether fbid exists in my database and if true sets session. If fbid does not exist script picks up additional data like username and email and then insert fbid into DB. Problem is my code inserts wrong id into db if fbid doesn't exist. Here is my code:

 <?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 ";

?>
  • 写回答

2条回答 默认 最新

  • douxun4924 2014-01-03 22:57
    关注

    Logic seems to be ok to fetch the user details using FQL. Now I see the section where you are adding the user into the DB

    $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>";
    

    Can you try

    $sqli=mysql_query("INSERT INTO users(`fbid`) VALUES('".$user_id."')");
    

    In addition you can debug by echoing the $user_id before doing the insert.

    Also you can use graph api calls instead of FQL to get the user info as

    $facebook->api('/me');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器