Skip to content

Instantly share code, notes, and snippets.

@yusufsyaifudin
Last active December 14, 2015 13:58
Show Gist options
  • Save yusufsyaifudin/5097451 to your computer and use it in GitHub Desktop.
Save yusufsyaifudin/5097451 to your computer and use it in GitHub Desktop.
Facebook login (dari docs facebook)
<?php
$app_id = "YOUR-APP-ID";
$app_secret = "YOUR-APP-SECRET-ID";
$my_url = "YOUR-CALLBACK-URL";
//$my_url = "http://yusyaif.com/facebook";
$code = $_REQUEST["code"];
session_start();
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri="
. urlencode($my_url) . "&state="
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['state'])) {
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret . "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$_SESSION['access_token'] = $params['access_token'];
$graph_url = "https://graph.facebook.com/me?access_token="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
echo("Hello " . $user->name);
}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment