I have a strange, but definitely serious problem regarding fetching data from some Facebook users who authorize my App and use it (normally by the way).
Around 3000 users have used the App so far, but only 600 are showing up in the users table. How could that be possible? I cannot find connection between old and new users – for some it works flawlessly, for others it just doesn’t work at all.
Here’s the complete, but for security reasons censored code:
<?php
require 'facebook.php';
include 'config.php';
session_start();
$app_id = 'id';
$app_secret = 'secret';
$app_namespace = 'namespace';
$app_url = 'https://apps.facebook.com/' . $app_namespace . '/';
$scope = 'email,publish_actions';
// Init the Facebook SDK
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
// Get the current user
$user = $facebook->getUser();
// If the user has not installed the app, redirect them to the Auth Dialog
if (!$user) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => $scope,
'redirect_uri' => $app_url,
));
print('<script> top.location.href=\'' . $loginUrl . '\'</script>');
}
else if ($user) {
try {
// Proceed knowing we have a logged in user who’s authenticated.
$fbuid = $facebook->getUser();
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
else{
header('Location: index.php');
}
$queryUserExists = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook'AND oauth_uid = " . $user_profile['id']);
$num=mysql_numrows($queryUserExists);
//If not, add it to the database
if ($num == 0) {
$query = mysql_query("INSERT INTO users (oauth_uid, oauth_provider, username, first_name, last_name, email, pic_square ,login_date , dailytokens)
VALUES ({$user_profile['id']}, 'facebook', '{$user_profile['name']}', '{$user_profile['first_name']}','{$user_profile['last_name']}','{$user_profile['email']}', '".$photolink."',CURDATE(),1)");
$query = mysql_query("SELECT * FROM users WHERE oauth_uid = " . mysql_insert_id());
$result = mysql_fetch_array($query);
}
else
{
$DateLastLogin=mysql_result($queryUserExists,0,"login_date");
$myDate = date('Y-m-d');
$CompLastDate = strtotime($DateLastLogin);
$CompMyDate = strtotime($myDate);
if($CompLastDate < $CompMyDate){
$updateDailyToken = "Update users set login_date=CURDATE(), dailytokens = dailytokens + 1 where oauth_uid =" . $user_profile['id'];
$updateToken= mysql_query($updateDailyToken) or die(mysql_error());
}
}
$query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook' AND oauth_uid = " . $user_profile['id']);
$_SESSION['userid']=$user_profile['id'];
// Login or logout url will be needed depending on current user state.
if ($user) {
$paramsout = array('next'=>'http://facebook.com');
$logoutUrl = $facebook->getLogoutUrl($paramsout);
}
?>
Problem was with the object type in the database. Some facebook users have incredebly large id: 0098788563000236 for example, so using BIGINT instead of INT solved it.