Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Facebook身份验证-新成员和现有成员?_Php_Facebook Graph Api_Facebook_Facebook Authentication - Fatal编程技术网

Php Facebook身份验证-新成员和现有成员?

Php Facebook身份验证-新成员和现有成员?,php,facebook-graph-api,facebook,facebook-authentication,Php,Facebook Graph Api,Facebook,Facebook Authentication,我刚刚使用Facebook JavaScript在我的网站上实现了Facebook身份验证 如果用户第一次从我的网站登录facebook,然后需要将数据插入users表(mysql)-可能是一个名为fb\u userid的字段和电子邮件 我已经阅读了JavaScript SDK上的文档,我可以获得Facebook用户ID,但如何将其传递到PHP中,以便插入到用户表中 Facebook Javascript如下所示: <script> window.fbAsyn

我刚刚使用Facebook JavaScript在我的网站上实现了Facebook身份验证

如果用户第一次从我的网站登录facebook,然后需要将数据插入
users
表(mysql)-可能是一个名为
fb\u userid
的字段和电子邮件

我已经阅读了JavaScript SDK上的文档,我可以获得Facebook用户ID,但如何将其传递到PHP中,以便插入到
用户
表中

Facebook Javascript如下所示:

    <script>
        window.fbAsyncInit = function() {
            FB.init({
                appId      : 'xxxxxxxx', // App ID
                status     : true, // check login status
                cookie     : true, // enable cookies to allow the server to access the session
                oauth      : true, // enable OAuth 2.0
                xfbml      : true  // parse XFBML
            });

        (function(d){
            var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
            js = d.createElement('script'); js.id = id; js.async = true;
            js.src = "//connect.facebook.net/en_US/all.js";
            d.getElementsByTagName('head')[0].appendChild(js);
        }(document));

window.fbAsyninit=函数(){
FB.init({
appId:'xxxxxxxx',//应用程序ID
状态:true,//检查登录状态
cookie:true,//启用cookie以允许服务器访问会话
oauth:true,//启用oauth 2.0
xfbml:true//解析xfbml
});
(职能(d){
var js,id='facebook jssdk';if(d.getElementById(id)){return;}
js=d.createElement('script');js.id=id;js.async=true;
js.src=“//connect.facebook.net/en_US/all.js”;
d、 getElementsByTagName('head')[0].appendChild(js);
}(文件);
在登录页面上:

  <fb:login-button scope="email" autologoutlink="true"></fb:login-button>

是否可以使用PHP SDK获取Facebook用户ID


在使用PHP SDK之前是否需要加载Facebook JavaScript文件?

在我看来,最好的选择是使用JavaScript(在我的示例中也是jQuery)在后台调用PHP页面,将用户添加到数据库中

页眉:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
function (data)
{
      file = 'adddata.php?data=' + data;
      $.get(file, function(content)
      {
          if (content == 'OK')
          {
              alert('Data added to the database!');
          }
          else
          {
              alert('Error adding data to the database!');
          }
      });
}
</script>

功能(数据)
{
文件='adddata.php?数据='+数据;
$.get(文件、函数(内容)
{
如果(内容=‘确定’)
{
警报(“数据已添加到数据库!”);
}
其他的
{
警报('Error adding data to the database!');
}
});
}
adddata.php的示例:



当然,您需要在PHP脚本中转义传入的数据,并稍微更改SQL查询,但这是一般的想法。

此代码获取用户数据并将其序列化,然后将其抛出数据库,我知道这并不完美,但请看一看。我将在有空闲时间时编辑此代码,然后建议将用户数据编码为JSON,而不是base64序列化,因为将来进行查询搜索会更容易

<?php
    require 'facebook.php'; // USE FACEBOOK PHP SDK

    // Create our Application instance (replace this with your appId and secret).
    $facebook = new Facebook(array(
      'appId'  => 'APPID',
      'secret' => 'APPSECRET',
    ));
    // ----------------------------------------------------------------------------------------
    // ----------------------------------------------------------------------------------------

    // Get User ID
    $user = $facebook->getUser();

    /* We may or may not have this data based on whether the user is logged in.
       If we have a $user id here, it means we know the user is logged into
       Facebook, but we don't know if the access token is valid. An access
       token is invalid if the user logged out of Facebook. */

    if ($user) {
      try {
        // Proceed knowing you have a logged in user who's authenticated.
        $dt = $facebook->api('/me');
        $lk = $facebook->api('/me/likes');
      } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
      }
    }

    // ----------------------------------------------------------------------------------------
    // ----------------------------------------------------------------------------------------
    // Handler for Login Status
    if ($user) {
      $logoutUrl = $facebook->getLogoutUrl();
    } else {
      $loginUrl = $facebook->getLoginUrl(array("scope" => "email,user_birthday,user_likes,user_work_history,user_location,user_education_history"));
    }
    // ----------------------------------------------------------------------------------------
    ?>
    <?php if (!$user): header ('Location:'.$loginUrl.''); //CHECKS IF USER IS LOGGED IN
    else: 
    $con = mysql_connect("DATABASE IP","USERNAME","PASS");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    else{
    $id = base64_encode(serialize($dt['id']));
    $work = base64_encode(serialize($dt['work']));  
    $location = base64_encode(serialize($dt['location']));  
    $education = base64_encode(serialize($dt['education']));    
    $f_n = $dt['first_name'];
    $l_n = $dt['last_name'];
    $link = $dt['link'];
    $u_n = $dt['username'];
    $bd = $dt['birthday'];
    $gdr = $dt['gender'];
    $em = $dt['email'];
    $sportsS = base64_encode(serialize($dt['sports']));
    $languagedt = $dt['languages'];
    foreach($languagedt as $item){
        $language[] = $item;
    }
    $languageS = base64_encode(serialize($language));

    $lk_dt = $lk['data'];
    foreach($lk_dt as $value)
      {
      if ($value['category'] == "Musician/band" || $value['category'] == "Song" ){
        $fav_music[] = $value;
      } 

      if($value['category'] == "Tv show"){
        $fav_tvshow[] = $value;
      }

      if($value['category'] == "Book"){
        $fav_book[] = $value;   
      }

      if($value['category'] == "Movie"){
        $fav_movie[] = $value;  
      }

      if($value['category'] == "Games/toys"){
        $fav_games[] = $value;  
      }

      if($value['category'] == "Interest"){
        $interest[] = $value;   

      }

      if($value['category'] == "Professional sports team"){
        $sport_team[] = $value; 

      }

      // next is likes
      if ($value['category'] != "Movie" && $value['category'] != "Musician/band" && $value['category'] != "Book" && $value['category'] != "Games/toys" && $value['category'] != "Tv show" && $value['category'] != "Professional sports team" && $value['category'] != "Interest" && $value['category'] != "Tv show" && $value['category'] != "Professional sports team" && $value['category'] != "Song") {
        $likes[] = $value;
      } 
    }
    $fav_musicS = base64_encode(serialize($fav_music));
    $fav_movieS = base64_encode(serialize($fav_movie));
    $fav_bookS = base64_encode(serialize($fav_book));
    $fav_tvshowS = base64_encode(serialize($fav_tvshow));
    $sport_teamS = base64_encode(serialize($sport_team));
    $fav_gamesS = base64_encode(serialize($fav_games));
    $interestS = base64_encode(serialize($interest));
    $likesS = base64_encode(serialize($likes));

    mysql_select_db("database", $con);

    $result = mysql_query("SELECT id, firstdone FROM users WHERE id='$id'");
    $row = mysql_fetch_array($result);
    $f_dn = $row['firstdone'];
    $ctrl = $row['id'];
    if (!$ctrl){

    mysql_query("INSERT INTO users (id, language, fav_sports, education, location, work, first_name, last_name, link, username, birthday, gender, email, fav_music, fav_movie, fav_books, fav_tv, fav_sport, fav_games, interest, likes)
    VALUES ('$id', '$languageS', '$sportsS', '$education', '$location', '$work', '$f_n', '$l_n', '$link', '$u_n', '$bd', '$gdr', '$em', '$fav_musicS', '$fav_movieS', '$fav_bookS', '$fav_tvshowS', '$sport_teamS', '$fav_gamesS', '$interestS', '$likesS')");      

    if ($_SERVER['REQUEST_URI'] == '/index.php') {
        header('Location: /theprofilepage');}
    }
    else{
    mysql_query("UPDATE users SET language='$languageS', fav_sports='$sportsS', first_name='$f_n', last_name='$l_n', link='$link', username='$u_n', birthday='$bd', gender='$gdr', email='$em', fav_music='$fav_musicS', fav_movie='$fav_movieS', fav_books='$fav_bookS', fav_tv='$fav_tvshowS', fav_sport='$sport_teamS', fav_games='$fav_gamesS', interest='$interestS', likes='$likesS', work='$work', location='$location', education='$education' WHERE id='$id'");

    }
    mysql_close($con);

    }endif
    ?>

<?php
    require 'facebook.php'; // USE FACEBOOK PHP SDK

    // Create our Application instance (replace this with your appId and secret).
    $facebook = new Facebook(array(
      'appId'  => 'APPID',
      'secret' => 'APPSECRET',
    ));
    // ----------------------------------------------------------------------------------------
    // ----------------------------------------------------------------------------------------

    // Get User ID
    $user = $facebook->getUser();

    /* We may or may not have this data based on whether the user is logged in.
       If we have a $user id here, it means we know the user is logged into
       Facebook, but we don't know if the access token is valid. An access
       token is invalid if the user logged out of Facebook. */

    if ($user) {
      try {
        // Proceed knowing you have a logged in user who's authenticated.
        $dt = $facebook->api('/me');
        $lk = $facebook->api('/me/likes');
      } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
      }
    }

    // ----------------------------------------------------------------------------------------
    // ----------------------------------------------------------------------------------------
    // Handler for Login Status
    if ($user) {
      $logoutUrl = $facebook->getLogoutUrl();
    } else {
      $loginUrl = $facebook->getLoginUrl(array("scope" => "email,user_birthday,user_likes,user_work_history,user_location,user_education_history"));
    }
    // ----------------------------------------------------------------------------------------
    ?>
    <?php if (!$user): header ('Location:'.$loginUrl.''); //CHECKS IF USER IS LOGGED IN
    else: 
    $con = mysql_connect("DATABASE IP","USERNAME","PASS");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    else{
    $id = base64_encode(serialize($dt['id']));
    $work = base64_encode(serialize($dt['work']));  
    $location = base64_encode(serialize($dt['location']));  
    $education = base64_encode(serialize($dt['education']));    
    $f_n = $dt['first_name'];
    $l_n = $dt['last_name'];
    $link = $dt['link'];
    $u_n = $dt['username'];
    $bd = $dt['birthday'];
    $gdr = $dt['gender'];
    $em = $dt['email'];
    $sportsS = base64_encode(serialize($dt['sports']));
    $languagedt = $dt['languages'];
    foreach($languagedt as $item){
        $language[] = $item;
    }
    $languageS = base64_encode(serialize($language));

    $lk_dt = $lk['data'];
    foreach($lk_dt as $value)
      {
      if ($value['category'] == "Musician/band" || $value['category'] == "Song" ){
        $fav_music[] = $value;
      } 

      if($value['category'] == "Tv show"){
        $fav_tvshow[] = $value;
      }

      if($value['category'] == "Book"){
        $fav_book[] = $value;   
      }

      if($value['category'] == "Movie"){
        $fav_movie[] = $value;  
      }

      if($value['category'] == "Games/toys"){
        $fav_games[] = $value;  
      }

      if($value['category'] == "Interest"){
        $interest[] = $value;   

      }

      if($value['category'] == "Professional sports team"){
        $sport_team[] = $value; 

      }

      // next is likes
      if ($value['category'] != "Movie" && $value['category'] != "Musician/band" && $value['category'] != "Book" && $value['category'] != "Games/toys" && $value['category'] != "Tv show" && $value['category'] != "Professional sports team" && $value['category'] != "Interest" && $value['category'] != "Tv show" && $value['category'] != "Professional sports team" && $value['category'] != "Song") {
        $likes[] = $value;
      } 
    }
    $fav_musicS = base64_encode(serialize($fav_music));
    $fav_movieS = base64_encode(serialize($fav_movie));
    $fav_bookS = base64_encode(serialize($fav_book));
    $fav_tvshowS = base64_encode(serialize($fav_tvshow));
    $sport_teamS = base64_encode(serialize($sport_team));
    $fav_gamesS = base64_encode(serialize($fav_games));
    $interestS = base64_encode(serialize($interest));
    $likesS = base64_encode(serialize($likes));

    mysql_select_db("database", $con);

    $result = mysql_query("SELECT id, firstdone FROM users WHERE id='$id'");
    $row = mysql_fetch_array($result);
    $f_dn = $row['firstdone'];
    $ctrl = $row['id'];
    if (!$ctrl){

    mysql_query("INSERT INTO users (id, language, fav_sports, education, location, work, first_name, last_name, link, username, birthday, gender, email, fav_music, fav_movie, fav_books, fav_tv, fav_sport, fav_games, interest, likes)
    VALUES ('$id', '$languageS', '$sportsS', '$education', '$location', '$work', '$f_n', '$l_n', '$link', '$u_n', '$bd', '$gdr', '$em', '$fav_musicS', '$fav_movieS', '$fav_bookS', '$fav_tvshowS', '$sport_teamS', '$fav_gamesS', '$interestS', '$likesS')");      

    if ($_SERVER['REQUEST_URI'] == '/index.php') {
        header('Location: /theprofilepage');}
    }
    else{
    mysql_query("UPDATE users SET language='$languageS', fav_sports='$sportsS', first_name='$f_n', last_name='$l_n', link='$link', username='$u_n', birthday='$bd', gender='$gdr', email='$em', fav_music='$fav_musicS', fav_movie='$fav_movieS', fav_books='$fav_bookS', fav_tv='$fav_tvshowS', fav_sport='$sport_teamS', fav_games='$fav_gamesS', interest='$interestS', likes='$likesS', work='$work', location='$location', education='$education' WHERE id='$id'");

    }
    mysql_close($con);

    }endif
    ?>