Skip to content

Instantly share code, notes, and snippets.

@motyar
Last active August 10, 2018 12:12
Show Gist options
  • Save motyar/25f07d7727541c7d7280a8a919c3932b to your computer and use it in GitHub Desktop.
Save motyar/25f07d7727541c7d7280a8a919c3932b to your computer and use it in GitHub Desktop.
Login with Facebook ( In 20 lines of PHP code )
<?php
// Set $apiVersion, $apiSecret, $redirectUrl, and $scope etc here.
if($_GET['logout']!=""){
session_destroy();
exit("Logged out");
}
if($_GET['code']!=""){
$accessData = json_decode(file_get_contents("https://graph.facebook.com/".$apiVersion."/oauth/access_token?client_id=".$appId."&redirect_uri=".$redirectUrl."&client_secret=".$appSecret."&code=".trim($_GET['code'])),true);
$userInfo = json_decode(file_get_contents("https://graph.facebook.com/v2.7/me?fields=email,id,name&access_token=".$accessData['access_token']),true);
$_SESSION['access'] = $userInfo;
}else{
if($_SESSION['access']['id']!=""){
echo 'Welcome '.$_SESSION['access']['name'].' <a href="?logout=true">Logout</a>';
}else{
?>
<a href="https://www.facebook.com/dialog/oauth?client_id=<?=$appId?>&redirect_uri=<?=$redirectUrl?>&scope=<?=$scope?>">Login with Facebook</a>
<?php
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment