Did Facebook just randomly change their API over the last couple of days? I had my site working perfectly with the Facebook API and now all of a sudden it doesn't work at all. No, I haven't changed anything, it literally just decided yesterday to not redirect anymore...it seems to just try to connect a few times and then it displays this page:
"The page isn't redirecting properly Firefox has detected that the server is redirecting the request for this address in a way that will never complete. * This problem can sometimes be caused by disabling or refusing to accept cookies."
Anyway, here is some code to wrap your heads around :P (yes, I do actually have my real app id's and such in place) This is the fbLogin_member.php file which is where you're directed after clicking on the Login link:
$app_id = "my id #";
$app_secret = "my secret id";
$my_url = "http://www.sitedomain.com/confirmlogin_fb_member.php";
if (empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=". $app_id ."&redirect_uri=". urlencode($my_url) ."&scope=email";
echo "<script> top.location.href='". $dialog_url ."'</script>";
}
$code = $_REQUEST['code'];
Here is the confirmlogin_fb_member.php file:
//if user denies access to your website, take him to your manual login page
// if ($_GET['error']) {
// header("Location: memberlogin.php");
// exit;
// }
require_once('config/dbconfig.php');
$app_id = "my id #";
$app_secret = "my secret id";
$my_url = "http://www.sitedomain.com/confirmlogin_fb_member.php";
$code = $_GET['code'];
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret="
. $app_secret . "&code=" . $code;
// request access token
//use curl and not file_get_contents()
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$access_token = curl_exec($ch);
curl_close($ch);
$graph_url = "https://graph.facebook.com/me?" . $access_token;
// request user data using the access token
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $graph_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$temp_user = curl_exec($ch);
curl_close($ch);
//decode the json array to get user data
$user = json_decode($temp_user);
//store user data
$u = $user->name;
$e = $user->email;
$fb_id = $user->id;
$username = $user->username;
$picture = 'https://graph.facebook.com/'. $fb_id .'/picture';
//check if user has already signed up before
$insert = true;
$res开发者_开发问答ult = mysql_query("SELECT * FROM members") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
//if username already exists, do not insert
if (($row['name'] == $u) && ($row['userType'] == "facebook_user")) {
$insert = false;
}
}
// Random Password Generator
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 7) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
// end generator
// if new user, insert user details in your mysql table
if ($insert) {
mysql_query("INSERT INTO members(name, fb_username, password, email, profile_pic, userType) VALUES('$u', '$username', '$pass' , '$e', '$picture', 'facebook_user')") or die(mysql_error());
}
//login user
if (!session_start()) session_start();
$_SESSION['in'] = true;
$_SESSION['userName'] = $u;
$_SESSION['userType'] = "facebook_user";
$_SESSION['userEmail'] = $e;
//take user to his/her homepage
header("Location: layout.php");
Lastly, here is the top of layout.php where the Facebook API session is called:
session_start();
require_once('config/dbconfig.php');
require_once('facebook/facebook.php');
if (isset($_REQUEST['logout'])) {
unset($_SESSION['in']);
unset($_SESSION['userName']);
unset($_SESSION['userType']);
unset($_SESSION['userEmail']);
session_destroy();
header("Location: layout.php");
}
$session = $_SESSION['in'];
if (!$session) {
$login = '<a href="fbLogin_member.php" class="fbLogin">Login with Facebook</a>';
$tooltipMsg = '<p>You must <strong>Log in</strong> to vote.</p>';
} else {
$sessionUser = $_SESSION['userName'];
$result = mysql_query("SELECT * FROM `members` WHERE name = '$sessionUser'") or die('Query failed: ' . mysql_error() . "<br />\n$sql");
if ($result) {
$sessionRow = mysql_fetch_array($result);
$sessionUserid = $sessionRow['memberid'];
}
if ($sessionRow['userType'] == "facebook_user") {
$facebook = new Facebook(array(
'appId' => 'my app id #',
'secret' => 'my secret id',
'cookie' => true
));
// $session = $facebook->getSession();
$user = $facebook->getUser();
$me = null;
// Session based API call.
if ($user) {
try {
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
// error_log($e);
}
}
}
It just boggles my mind that it has worked fine for a couple weeks, and now when I come back home from being gone a day and a half it doesn't work. Any help would be appreciated, even if there is some other things wrong you see with my coding (this is my first attempt with the facebook api) :)
It sounds like you might have an infinite redirect problem?
You are checking to see if $code is set before assigning it, try flipping the order of these two statements:
$code = $_REQUEST['code'];
if (empty($code)) { $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=". $app_id ."&redirect_uri=". urlencode($my_url) ."&scope=email"; echo " top.location.href='". $dialog_url ."'"; }
You might need to change $_REQUEST to $_GET in protected function getCode() in base_facebook.php
protected function getCode() {
if (isset($_GET['code'])) {
if ($this->state !== null &&
isset($_GET['state']) &&
$this->state === $_GET['state']) {
// CSRF state has done its job, so clear it
$this->state = null;
$this->clearPersistentData('state');
return $_GET['code'];
} else {
self::errorLog('CSRF state token does not match one provided.');
return false;
}
}
return false;
}
精彩评论