Page 1 of 1

Facebook Login has problem adding users.

Posted: Thu Sep 22, 2011 9:12 am
by tkelting08
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>

Code: Select all

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

Code: Select all

<?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>';
}
?>

Code: Select all

<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>

Re: Facebook Login has problem adding users.

Posted: Fri Sep 23, 2011 3:21 am
by twinedev
On your insert line, try changing it to this to determine what error you may be getting when trying to insert the value:

Code: Select all

$SQL = "INSERT INTO tz_members(facebook_id,usr,regIP,dt)
                                                VALUES(
                                               
                                                        '".$fbookid."',
                                                        '".$fbookname."',
                                                        '".$_SERVER['REMOTE_ADDR']."',
                                                        NOW()
                                                       
                                                )";

mysql_query($SQL) or die ('Error with: '.$SQL.'<hr>'.mysql_error() );
This way when it fails, you can see the exact SQL it was trying to use to insert a new record, and what error it received.

-Greg