dongyishe6689 2016-10-20 07:44
浏览 96

首次尝试获取会话时,Facebook登录失败

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>"; 
}
  • 写回答

1条回答 默认 最新

  • dongzhi1822 2016-10-21 10:57
    关注

    This can be closed now. Fixed everything by using the latest version of Facebook's PHP SDK. Thanks!

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大