I am planning to integrate a Facebook Login button into my website. Right now I have a Facebook Login button that when pressed, popups a new window asking for permission to access the user's basic info and email. Everything done till now is from following http://developers.facebook.com/docs/guides/web/#login. I am using PHP (with Codeigniter framework, Tank auth authentication library) and jQuery.

Problem: Now after adding the login button to my website, Facebook no longer have any instructions on how to proceed?

  1. How do I retrieve the user's information from facebook to insert into my database (if first visit)
  2. How do I determine that the user has allowed my website to access his information?
  3. If access has been granted, how do I redirect the user to a new page?

I understand there have been recent changes to the SDK, and googling for tutorials on it just gives me tutorials that says they are outdated. Any help to point me in the right direction will be greatly appreciated! Thanks!

HTML/JS

<div id="fb-root"></div>
<script>
    window.fbAsyncInit = function() {
        FB.init({
            appId      : '123',
            status     : true, 
            cookie     : true,
            xfbml      : true,
            oauth      : true,
        });
    };
    (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));
</script>

<div id="splash_fbconnect_login_button" class="fb-login-button" data-scope="email, user_education_history, user_work_history"></div>
有帮助吗?

解决方案

Quick edit: Go to https://github.com/facebook/connect-js make sure you have the latest version and use the PHP one, it makes it a hell of a lot easier i found.

1) How do I retrieve the user's information from facebook to insert into my database (if first visit)

$user_profile['last_name'];
$user_profile['first_name'];
$user_profile['gender']; 

etc.

2) How do I determine that the user has allowed my website to access his information?

The facebook API knows this, dont worry :-) as when the user clicks login a popup comes up (as you mentioned in your question) and when they click allow its done.

One thing you must do however is create an App in facebook otherwise it wont work, im not sure if you have done this or not so go to https://developers.facebook.com/apps

3)If access has been granted, how do I redirect the user to a new page?

Javascript redirect in an if statement, this is what mine looks like

Be sure to set some variables here too:

   <?php // Set the variables for the MVC
      $first_name = $user_profile['first_name'];
      $last_name = $user_profile['last_name'];
      $full_name = $first_name . " " . $last_name;
      $fb_id = $user_profile['id'];
      $gender = $user_profile['gender'];



       $SrcUser = mysql_query("SELECT * FROM users WHERE fb_id = '" . $fb_id . "'") or die(mysql_error());
      $CheckDB = mysql_num_rows($SrcUser);

      if($CheckDB == 1){
          // If user has an account

          while ($row = mysql_fetch_array($SrcUser)) 
                { 
                     $id = $row["id"];
                     $user_level = $row["user_level"];
                }
        session_start();
        $_SESSION['user_name'] = $full_name;
        $_SESSION['user_level'] = $user_level;
        $_SESSION['user_id']= $id;  
        // Javascript redirect.. its kinda cheating to be honest as if i dont use it i will have awkwardness with header errors
        ?>
  <script type="text/javascript">
        window.location = "myaccount.php"
        </script>
        <?
      } else {
          // If user does not have an account create new
        ?>  <?
        session_start();
        $_SESSION['fb_id']= $fb_id;  

        if($gender == "male"){
            $gender = "1";
        } else {
            $gender = "0";
        }
        $_SESSION['r_full'] = $full_name;
        $_SESSION['r_gender'] = $gender;

        mysql_query("INSERT INTO users (full_name, gender, fb_id)
            VALUES ('$full_name','$gender','$fb_id')");
        $user_id = mysql_insert_id();  
        $md5_id = md5($user_id);
        mysql_query("update users set md5_id='$md5_id' where id='$user_id'");

            $md5_id = md5($user_id);
            mysql_query("update users set md5_id='$md5_id' where id='$user_id'");

        ?>

Hope this helps!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top