How to login with facebook on website? Reviewed by Momizat on . First creat a App on Facebook go to:- https://developers.facebook.com Get Application ID and App secret PHP code fb-login.php page on website [php] include First creat a App on Facebook go to:- https://developers.facebook.com Get Application ID and App secret PHP code fb-login.php page on website [php] include Rating: 0

How to login with facebook on website?

First creat a App on Facebook go to:-

https://developers.facebook.com

Screen Shot 2014-05-01 at 15.52.38

Get Application ID and App secret

Screen Shot 2014-05-01 at 15.55.45

PHP code fb-login.php page on website

include "facebook.com";
$udetails=array();
$app_id = "xxxxxxxxxxxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_url = "http://www.site.com/fb-login.php";
$scope = "email,publish_stream,offline_access,user_education_history, friends_education_history,user_birthday,friends_birthday,user_likes,friends_likes,user_work_history, friends_work_history";
$code = (isset($_GET["code"])?$_GET["code"]:'');
if(empty($code)) {
 $_SESSION["state"]=md5(uniqid(rand(), TRUE));
 $session_id = $_SESSION["state"];
 $dialog_url = "https://www.facebook.com/dialog/oauth"
 ."?client_id=".$app_id
 ."&redirect_uri=".urlencode($my_url)
 ."&state=".$session_id
 ."&scope=".$scope;
 header("Location: " . $dialog_url);
}
$session_id = $_SESSION["state"];
 if($session_id === $_GET['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));
 if(!empty($user)){
 //echo "
";print_r($user);
 $udetails['email']= (isset($user->email)?$user->email:'');
 $udetails['users_fname']= (isset($user->first_name)?$user->first_name:'');
 $udetails['users_lname']= (isset($user->last_name)?$user->last_name:'');
 $udetails['gender']= (isset($user->gender)?$user->gender:'');
 $udetails['id']= (isset($user->id)?$user->id:'');
 $udetails['img']="https://graph.facebook.com/".$user->id."/picture?type=large";
 }
 print_r($udetails);
 }else {
 echo("The state does not match.");
 }

Comments (1)

Leave a Comment

© 2014 Powered By