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 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改