<!--Add in Header / where login -->
<button class="facebook" id="facebook"><i class="fa fa-facebook" aria-hidden="true"></i>Sign in with Facebook</button>
<!-- Add in Header / where login -->
## Add this to the same login page or footer page.
<script type="text/javascript">
window.fbAsyncInit = function() {
//Initiallize the facebook using the facebook javascript sdk
FB.init({ appId:'781510555361020', // App ID
cookie:true, // enable cookies to allow the server to access the session
status:true, // check login status
xfbml:true, // parse XFBML
oauth : true //enable Oauth
});
};
//Read the baseurl from the config.php file
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
//Onclick for fb login
$('#facebook').click(function(e) {
FB.login(function(response) { if(response.authResponse) {
// parent.location ='fblogin.php'; //redirect uri after closing the facebook popup
$.ajax({ type:"post",
url: "fblogin.php",
data:{ response: response,}, success:function(response)
{ if(response==false){ location.reload();
}else{
window.location = response;
}
}
});
}
},
{scope: 'email,public_profile'});
//permissions for facebook
});
</script>##other Page: fblogin.php
$response = $_POST['response'];
$authResponse = $response['authResponse'];
$facebook_user_data_url = "https://graph.facebook.com/".$authResponse['userID']."/?fields=email,first_name,last_name&access_token=".$authResponse['accessToken'];
/*
$facebook_user_data = json_decode(file_get_contents($facebook_user_data_url));
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $facebook_user_data_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
$facebook_user_data = json_decode($data);
//Checking if this user already registered with Facebook.
$user_ifexits = mysql_query("SELECT * FROM user WHERE email='$facebook_user_data->email'");
if(mysql_num_rows($user_ifexit)>0){
//Start user session here.
// His my account redirect link
$url = "account.php";
echo $url;
}else{
//Insert user record and register then redirect his to his account.
$_SESSION['msg'] = "Login Unsuccess";
echo false;
}