I'm having issues with my Facebook Login using PHP SDK.
I have if else statement for the session variables as I also use this login to store user info to db, like name, email and uid.
Problem I'm getting is on first login, code will always execute the ELSE statement for the session which executes the the script that says "Please try to login again", then when the user try to login again, it will be successful. I need help fixing this so user will be recognized on first attempt.
Also, my login button is being accessed via:
href="example.org/fb-redirect.php"
Thanks!
<?php
include('debug.php');
require_once(dirname(__FILE__)."/templates/initialize.inc");
session_start();
$curtimestamp = date('Y-m-d H:i:s'); // added in v4.0.0
require_once 'fb/autoload.php';
require_once('fb/src/Facebook/GraphUser.php');
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest; use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
function authoLogin($userLogin){
$global_connection = db_connect();
$userdata = db_getrow("select * from user where user_login like '".$userLogin."'", $global_connection);
db_query("update user set user_loginerror=0 where user_id='".$userdata['user_id']."'", $global_connection);
util_setsessionvar('usertype',$userdata['usertype_id']);
util_setsessionvar('userid',$userdata['user_id']);
util_setsessionvar('userlogin',$userdata['user_login']);
util_setsessionvar('username',$userdata['user_name']);
}
// init app with app id and secret
FacebookSession::setDefaultApplication( 'appid','secret' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('http://example.org/fb-redirect.php' );
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
}
if ( !isset( $session ) || $session === null ) {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
// handle this better in production code
print_r( $ex );
} catch( Exception $ex ) {
// When validation fails or other local issues
// handle this better in production code
print_r( $ex );
}
}
// see if we have a session
// see if we have a session
if ( isset( $session ) ) {
//store the token in the php session
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject(GraphUser::classname());
$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 $fgetid=$graphObject->getID(); //get image source from facebook account
$getimgsrc='https://graph.facebook.com/'.$fgetid.'/picture?width=200';
/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $femail; $_SESSION['PROFILE_PICTURE']=$getimgsrc; $_SESSION ['form_data']['return_url'] = $_SERVER['HTTP_REFERRER'];
if statement {
//save to database
} else if statement{
}
/* ---- header location after session ----*/
}else{
echo "<script> alert('Please try to login again'); window.location.href='index.php'; </script>";
}