Skip to content

Instantly share code, notes, and snippets.

@mogya
Last active December 16, 2015 08:28
Show Gist options
  • Save mogya/5405734 to your computer and use it in GitHub Desktop.
Save mogya/5405734 to your computer and use it in GitHub Desktop.
facebook login with Facebook PHP SDK (v.3.2.2)
<?
include_once("facebook/facebook.php"); //include facebook SDK
function myurl(){
if ( isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] == 'on' ){
$protocol = 'https://';
}else{
$protocol = 'http://';
}
return $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
//Call Facebook API
$facebook = new Facebook(array(
'appId' => '*****',
'secret' => '*****'
));
$user = $facebook->getUser();
if ($user) {
error_log('got user!');
$user_profile = $facebook->api('/me');
}else{
error_log('go facebook to login!');
$callback = myurl();
$loginUrl = $facebook->getLoginUrl(array('redirect_uri' => $callback, 'scope' => 'email,publish_actions'));
header("HTTP/1.1 307 Temporary Redirect");
header("Location: {$loginUrl}");
exit();
}
?>
<html><head></head>
<body>
<?php
echo "Hello {$user_profile['name']}"
?>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment