google / facebook登录成功后如何导航到同一页面?

时间:2016-05-30 04:58:45

标签: php

我使用php创建了一个网站,谷歌和Facebook登录。这个网站有6页。对于所有6页页眉和页脚是常见的(包括在内)。假设我使用谷歌从第5页登录网站。成功/失败登录后,页面导航到index.php(页面1)而不是page5。如何在google和facebook成功/失败登录后导航到同一页面。如果不添加google / facebook开发者控制台中的所有页面,这是否可行?

我也尝试在成功登录后更改标题位置。抛出无法达到,出现时间错误。任何人都可以帮助解决这个问题。提前致谢。

注意:我正在使用google和facebook Oauth服务进行登录。

这是我的代码: 谷歌登录。

<?php

session_start();
require_once 'dbconnection.php';

//Google API PHP Library includes
require_once  'gvendor/vendor/autoload.php';
require_once 'gvendor/vendor/google/apiclient/src/Google/Client.php';
require_once 'gvendor/vendor/google/apiclient/src/Google/Service/Oauth2.php';

// Fill CLIENT ID, CLIENT SECRET ID, REDIRECT URI from Google Developer Console
 $client_id = 'xxxxxxx';
 $client_secret = 'xxxxxxx';
 $redirect_uri = 'http://localhost:80/tthtml/index.php';
 $simple_api_key = 'xxxxxxx';

 global $googleauthUrl;

//Create Client Request to access Google API
$client = new Google_Client();
$client->setApplicationName("PHP Google OAuth Login Example");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setDeveloperKey($simple_api_key);
$client->addScope("https://www.googleapis.com/auth/userinfo.email");
$client->setAccessType('offline');


//Send Client Request
$objOAuthService = new Google_Service_Oauth2($client);

try{
//Logout
if (isset($_REQUEST['logout'])) {
  $client->revokeToken($_SESSION['access_token']);
  unset($_SESSION['access_token']);
  unset($_SESSION['google_user_name']);
  session_unset();
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); //redirect user back to page
}

//Authenticate code from Google OAuth Flow
//Add Access Token to Session
if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

//Set Access Token to make Request
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
}

//Get User Data from Google Plus
//If New, Insert to Database
if ($client->getAccessToken()) {
  $userData = $objOAuthService->userinfo->get();
  $_SESSION['google_user_name']=$userData['given_name'];
  $_SESSION['user_id']=$userData->id;

        if(!empty($userData)) {
            $dbObj=new database();  
            $dbObj->openconnection();
             $sql='select * from tttbl_user where google_fb_id='.$userData->id;
               $existing_member = $dbObj->existingMember($sql);
               if(empty($existing_member)) {
                   $sql="insert into tttbl_user (google_fb_id, user_name, gender, email_id, gplus_link, profile_photo, created_date) values('".$userData->id."','".$userData->name."','".$userData->gender."','".$userData->email."','".$userData->link."','".$userData->picture."',now())";
                   $dbObj->newUser($sql);
              }
              $dbObj->closeconnection();
         }

  } 
else{
        $googleauthUrl = $client->createAuthUrl();
    }
}
 catch(Exception $ee)
 { }

?>

facebook登录

<?php //
//ob_start();
session_start();
require_once 'dbconnection.php';
require_once 'fvendor/vendor/autoload.php';
require_once 'fvendor/vendor/facebook/php-sdk/src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
global $facebook_loginUrl;

$appId='xxxxxx';
$secretkey='xxxxxx';

$facebook = new Facebook(array(
  'appId'  => 'xxxxxxx',
  'secret' => 'xxxxxxxx',
));

// Get User ID
$fb_user = $facebook->getUser();

if(isset($_REQUEST['fb_logout'])){
    //$accessToken=null;
    //$logoutUrl = $helper->getLogoutUrl($_SESSION['facebook_access_token'], 'http://localhost/fblogin/fblogin.php');
    //unset($_SESSION['facebook_access_token']);
    unset($_SESSION['facebook_user_name']);
    session_unset();
    session_destroy();
    $fb_user = null;
    header('Location: http://localhost:80/tthtml/index.php');  
}


if ($fb_user) {
  try {
        // Proceed knowing you have a logged in user who's authenticated.
       $uid = $facebook->getUser();
       $user_profile = $facebook->api('/me?fields=id,name,picture,email,gender');

    }
    catch (FacebookApiException $e) {
      error_log($e);
      $fb_user = null;
    }
}
else 
{
    $facebook_loginUrl = $facebook->getLoginUrl(array('scope' => 'email,user_birthday,user_photos','req_perms' => 'user_mobile_phone',));
        $facebookurlstring=$facebook_loginUrl;
        $x=strpos($facebookurlstring,'redirect_uri=');
        $y=strpos($facebookurlstring, 'state');
        $facebookurllength=  strlen($facebookurlstring);
        //substr($str,0,$y+13).'http://localhost/tthtml/index.php'.substr($str, $y+13+$x,$length)
        $facebook_loginUrl=substr($facebookurlstring,0,$x+13).'http://localhost/tthtml/index.php'.substr($facebookurlstring, $y-1,$facebookurllength);
}

if($fb_user) 
{
        $userid=$user_profile['id'];
    $username = $user_profile['name'];        
    $useremail = $user_profile['email'];

        $userpicture=$user_profile['picture']['data']['url'];
        $usergender=$user_profile['gender'];
        $mobilenumber=$user_profile['user_mobile_phone'];

        $_SESSION['facebook_user_name']=$username;
    $_SESSION['user_id']=$userid;

        if(isset($_SESSION['facebook_user_name'])&& $_SESSION['facebook_user_name']) {
            $dbObj=new database();  
            $dbObj->openconnection();
            $sql='select * from tttbl_user where google_fb_id='.$userid.';';
               $existing_member = $dbObj->existingMember($sql);
               if(empty($existing_member)) {
                   $sql="insert into tttbl_user (google_fb_id, user_name, gender, email_id, profile_photo, created_date)"
                           . " values('".$userid."','".$username."','".$usergender."','".$useremail."','".$userpicture."',now())";
                   $dbObj->newUser($sql);
              }
              $dbObj->closeconnection();

         }
} 
?>

如果我更改了重定向URI,它将在开发者控制台的URL列表中进行搜索。如果它在开发者控制台列表中不可用,那么它会抛出ulr在白名单中不可用(对于Facebook)并且找不到页面(对于谷歌)错误。

1 个答案:

答案 0 :(得分:0)

尝试使用jQuery代码重定向

echo "<script>window.location.href ='yourpage.php';</script>";