Jump to content

Sync Facebook with mysql database/PHP problem.


tk_ink

Recommended Posts

Hey there I'm trying to gather a users Facebook info and use that to create a user account for them on my database.

 

My code works perfectly for the first person who links his Facebook account with my site and adds all the user info to the database. After that with any other users it will display the logged in Facebook users name with their profile pic. The problem arises when it tries to add this person to the mysql database. It returns the error "An error has occurred and we are unable to sync your Facebook account."

 

Before the <head>

 

<?php
define('FACEBOOK_APP_ID', 'imagine_app_id_here');
define('FACEBOOK_SECRET', 'imagine_secret_here');
function get_facebook_cookie($app_id, $application_secret) {
  $args = array();
  parse_str(trim($_COOKIE['fbs_' . $app_id], '"'), $args);
  ksort($args);
  $payload = '';
  foreach ($args as $key => $value) {
    if ($key != 'sig') {
      $payload .= $key . '=' . $value;
    }
  }
  if (md5($payload . $application_secret) != $args['sig']) {
    return null;
  }
  return $args;
}
$cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);
?> 

 

In the <body>

 

<?php if ($cookie) {
//###cookie is set, user is logged in
$user = json_decode(file_get_contents('https://graph.facebook.com/'.$cookie['uid']));


define('INCLUDE_CHECK',true);
require("connect.php");

$fbookid = $user->{'id'};
$fbookname = $user->{'name'};


$query = mysql_query("SELECT * FROM tz_members WHERE facebook_id='$fbookid'");
        $numrows = mysql_num_rows($query);
        if ($numrows == 1){
                echo "<font color='#FF0000'>You are logged in through Facebook.</font>";
        }
        else
        {
                        mysql_query("INSERT INTO tz_members(facebook_id,usr,regIP,dt)
                        VALUES(
                        
                            '".$fbookid."',
                            '".$fbookname."',
                            '".$_SERVER['REMOTE_ADDR']."',
                            NOW()
                            
                        )");
                        $query = mysql_query("SELECT * FROM tz_members WHERE facebook_id='$fbookid'");
                        $numrows = mysql_num_rows($query);
                        if ($numrows == 1){
    
                                echo "<font color='#FF0000'>Your Facebook account has been synced with our user database.</font>";
                        }
                        else {

                            echo "<font color='#FF0000'>An error has occured and we are unable to sync your Facebook account.</font>";
                        }
                        mysql_close();
        }
                                        
echo '<img src="http://graph.facebook.com/'.$user->{'id'}.'/picture" alt="'.$user->{'name'}.'"/>';
echo $user->{'name'};

echo '<fb:login-button perms="email,user_birthday" onlogin="window.location.reload(true);" autologoutlink="true"></fb:login-button>';


}
else
{
//###user is not logged in, display the Facebook login button
echo '<fb:login-button perms="email,user_birthday" autologoutlink="true"></fb:login-button>';
}
?> 

 

Javascript:

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({appId: '<?= FACEBOOK_APP_ID ?>', status: true,
cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
</script>

 

Any help would be greatly appreciated!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.