I'm using php facebook sdk on my website I have two subfolders each has a different database but after facebook auth it redirects to the main url of the site and does not save the data to the correct database. The data should be submitted to header("Location: https://123.com/uae/user/fblogin"); but insteaded it is redirected to header("Location: https://123.com/user/fblogin");
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me?locale=en_US&fields=id,name,email,first_name,last_name' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name');
$femail = $graphObject->getProperty('email'); // To Get Facebook full name
$femail = $graphObject->getProperty('email'); // To Get Facebook email ID
$first_name = $graphObject->getProperty('first_name'); // To Get Facebook email ID
$last_name = $graphObject->getProperty('last_name'); // To Get Facebook email ID
/* ---- Session Variables -----*/
$_SESSION['FB']['fbid'] = $fbid;
$_SESSION['FB']['fullname'] = $fbfullname;
$_SESSION['FB']['email'] = $femail;
$_SESSION['FB']['first_name'] = $first_name;
$_SESSION['FB']['last_name'] = $last_name;
//echo '<pre>'; print_r($_SESSION);die;
if(isset($_SESSION['connect']) && $_SESSION['connect'] == 1){
unset($_SESSION['connect']);
header("Location: https://123.com/uae/fbconnect/");
}else{
header("Location: https://123.com/uae/user/fblogin");
exit;
}
Related
I am trying to automatically redirect the user to a URL after the ReCaptcha was successful.
<?php
require('src/autoload.php');
$siteKey = 'my-site-key';
$secret = 'my-secret-key';
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$gRecaptchaResponse = $_POST['g-recaptcha-response']; //google captcha post data
$remoteIp = $_SERVER['REMOTE_ADDR']; //to get user's ip
$recaptchaErrors = ''; // blank varible to store error
$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp); //method to verify captcha
if ($resp->isSuccess()){
header('Location: http://www.theurl.com/'); //this is where it should redirect to
exit;
}
else {
$recaptchaErrors = $resp->getErrorCodes(); // set the error in varible
}
?>
I have searched almost the whole of Google to no avail.
While login wiht fb php sdk, I am getting such kind of error
This page isn’t working
www.facebook.com is currently unable to handle this request.
HTTP ERROR 500
I had configure my app and my secret key properly according to facebook app and key.
I am not getting why such kind of error is thrown. Please Help me
// init app with app id and secret
FacebookSession::setDefaultApplication(
myappkey,mysecretkey );
// login helper with redirect_uri
$helper = new
FacebookRedirectLoginHelper('http://demos.krizna.com/1353/fbconfig.php' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
$femail = $graphObject->getProperty('email'); // To Get Facebook email ID
/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $femail;
/* ---- header location after session ----*/
header("Location: index.php");
} else {
$loginUrl = $helper->getLoginUrl();
header("Location: ".$loginUrl);
}
This question already has answers here:
Facebook JS SDK's FB.api('/me') method doesn't return the fields I expect in Graph API v2.4+
(4 answers)
Closed 6 years ago.
How to get email and contact from facebook api. $graphObject gives only
name and id. how to solve this?
if( isset( $session ) ) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
$femail = $graphObject->getProperty('email'); // To Get Facebook email ID
echo "<pre>";print_r($graphObject); exit;
/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['FBFULLNAME'] = $fbfullname;
$_SESSION['FBEMAIL'] = $femail;
/* ---- header location after session ----*/
header("Location: index.php?act=fbmobile");
}
This would be the correct API call:
/me?fields=name,email
Since v2.4 of the Graph API, you have to specify the fields. It is called "Declarative Fields".
I have code like this:
$helper = new FacebookRedirectLoginHelper('http://MyAwesomeWebsite.com/' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
echo print_r($ex);
} catch( Exception $ex ) {
echo print_r($ex);
}
if ( isset( $session ) ) {
$request = new FacebookRequest( $session, 'GET', '/me?locale=en_US&fields=name,email' );
$response = $request->execute();
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
$femail = $graphObject->getProperty('email'); // To Get Facebook email ID
}
But every time $femail is null. Why? What I'm doing wrong?
When you log in the user, you have to pass along the email permission in the scope parameter.
So when you call your helper, also initialise a $permissions array:
$helper = new FacebookRedirectLoginHelper('http://MyAwesomeWebsite.com/' );
$permissions = ['email', 'public_profile'];
There should be $helper->getLoginUrl call in somewhere in your code. In that call you should pass the $permissions variable as the second parameter:
$loginUrl = $helper->getLoginUrl($placeYourCallbackURLHere, $permissions);
That asks the user explicitly for a permission to get the user's email. Note that the email can also be empty, if the user has:
Registered to Facebook via phone number (does not have email address in Faceebook)
Selected that they do not want to provide the email address at the login prompt.
I am creating a user log in on my page with the option to use facebook to log in using the PHP SDK. The problem I am having is that facebook leaves my site, gets permission from user and facebook and then comes back to my site. Everything works as it should, but the token facebook creates is stored in the url, such as www.############/account/register.php?code=AQDEN90jFQLsDPS5UEXOWCBItXfs5uVbKDs2AfZmV5d7nIHTNy6IXUXtDX-BJMxh6wlsC6-QoOzxnGCLqD_lG4Ui0HF5eUwLP15fUrAgwrwDLHcmRUKrvIcgHOOxr61u6E8me1EV7-VAHiRXG-D-U7qi8fWS7fYpU5OOjGctdzoOvx9Vl35NpXbbALq&state=5659508109f2207707c8#=
If the user refreshes that page after being redirected, its screws the whole thing up because it is resubmitting the token again which throws php facebook errors and ends the code chain.
My code is below.
//1.Use app id,secret and redirect url
$app_id = '###################';
$app_secret = '#########';
$redirect_url='http://#####account/register.php';
//2.Initialize application, create helper object and get fb sess
FacebookSession::setDefaultApplication($app_id,$app_secret);
$helper = new FacebookRedirectLoginHelper($redirect_url);
$sess = $helper->getSessionFromRedirect();
//check if facebook session exists
if(isset($_SESSION['fb_token'])){
$sess = new FacebookSession($_SESSION['fb_token']);
}
//logout
$logout = 'http:/########/includes/fblogin/lib/Facebook/FbLogout.php';
//3. if fb sess exists echo name
if(isset($sess)){
//store the token in the php session
$_SESSION['fb_token']=$sess->getToken();
//create request object,execute and capture response
$request = new FacebookRequest($sess, 'GET', '/me');
// from response get graph object
$response = $request->execute();
$graph = $response->getGraphObject(GraphUser::className());
$name = $graph->getName();
$id = $graph->getId();
$image = 'https://graph.facebook.com/'.$id.'/picture';
$email = $graph->getProperty('email');
echo "hi $name <br>";
echo "your email is $email <br><Br>";
echo "<img style='width: 50px; border-radius: 4px;' src='$image' /><br><br>";
echo "<a href='".$logout."'>Logout</a>";
}else{
//else echo login
echo '<a href='.$helper->getLoginUrl().'>Login with facebook</a>';
}
So, how can I store the request token in the $sess variable on return from facebook, and then clear the url so that the user cant refresh with that string in the url?
Got it!! Simple PHP solution...
if (!empty($_GET)){
$_SESSION['got'] = $_GET;
header("Location: http://##############/account/register.php");
}
else{
if (!empty($_SESSION['got'])){
$_GET = $_SESSION['got'];
unset($_SESSION['got']);
}