I'd like users on my site to be able to log in using their existing Facebook accounts.
I've downloaded the Facebook PHP SDK from developers.facebook.com and set up an app for my website. I just used the example.php file that came bundled with the sdk, and swapped out the numbers for the 'appID', and 'secret' of my app.
NOTE: In code 'appID' and 'secret' are replacing the actual equivalent
I have this is my header:
require 'src/facebook.php'; (Which is definitely being found)
This is the code on the main page, where the login will be (also from example.php):
<?php
$facebook = new Facebook(array(
'appId' => 'appID',
'secret' => 'secret',
));
var_dump($facebook);
// Get User ID
$user = $facebook->getUser();
echo "<br />";
var_dump($user);
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
$naitik = $facebook->api('/naitik');
?>
<?php var_dump($user); ?>
<h1>php-sdk</h1>
<?php if ($user): ?>
<a href="<?php echo $logoutUrl; ?>">Logout</a>
<?php else: ?>
<?php echo "LoginURL:"; var_dump($loginUrl);?>
<div>
<a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>
When I'm not logged into Facebook, and click the link, it takes me to a Facebook login page. After logging in, it then redirects back to the main site with new variables in the url (state, code, etc.). Once "logged" in, var_dump($user) just returns int(0).
Would anyone be able to point me in the right direction? I have no clue why it can't assign a user.
Result of dumping $facebook:
object(Facebook)#182 (9) {
["sharedSessionID:protected"]=> NULL
["appId:protected"]=> string(15) "appID"
["appSecret:protected"]=> string(32) "secret"
["user:protected"]=> NULL ["signedRequest:protected"]=> NULL
["state:protected"]=> NULL ["accessToken:protected"]=> NULL
["fileUploadSupport:protected"]=> bool(false)
["trustForwarded:protected"]=> bool(false)
}