Last active
December 16, 2015 08:28
-
-
Save mogya/5405734 to your computer and use it in GitHub Desktop.
facebook login with Facebook PHP SDK (v.3.2.2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
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