1

我正在使用 google auth2 进行用户登录,如果用户不存在于数据库中,则获取用户信息并将其保存到数据库中......以下是代码,在这里您可以看到错误。当我删除 dooperation() 函数时,它不会给出错误....functions.php 包含名为“User”的类,我在其中检查用户是否存在,如果不存在则添加行..它为每一行给出错误dooperation() 函数......这里有什么问题,如果我做错了什么,还告诉我......谢谢

<?php

require_once 'src/apiClient.php';
require_once 'src/contrib/apiOauth2Service.php';
require_once 'config/functions.php';
session_start();

$client = new apiClient();
$client->setApplicationName("Google Account Login");
$oauth2 = new apiOauth2Service($client);

if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];

header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {
 $client->setAccessToken($_SESSION['token']);
}

if (isset($_REQUEST['logout'])) {
 unset($_SESSION['token']);
 unset($_SESSION['google_data']); //Google session data unset
 $client->revokeToken();
 header('Location: home.php');
 exit();
}
   function dooperation($user){
      $ret=false;
      $email = $user['email'];
      $fname = $user['given_name'];
      $lname = $user['family_name'];

      $gender=$user['gender'];
      $dob = $user['birthday'];
      $location = null;
      $picture = $user['picture'];
      $uid = $user['id'];
      $ousername = null;
      $link = $user['link'];
           $provider = 'google';
           $verified = $user['verified_email'];

          $gen=0;
          if($gender == 'male'){$gen=1;} else{$gen=0;}
          $new_dob = date( "Y-m-d H:i:s", strtotime($dob) );

          $userObj = new User();
          $userdata = $userObj->checkUser($email,$fname,$lname,$gen,$new_dob,$location,$picture,$uid,$ousername,$link,$provider,$new_verified); 
          if(!empty($userdata)){         
            $_SESSION['id'] = $userdata['user_id'];
            $_SESSION['oauth_id'] = $userdata['user_oauth_id'];
            $_SESSION['username'] = $userdata['user_oauth_username'];
            $_SESSION['picture'] = $userdata['picture'];            
            $_SESSION['link'] = $userdata['link'];
            $_SESSION['fname'] = $userdata['user_fname'];
            $_SESSION['lname'] = $userdata['user_lname'];
            $_SESSION['gender'] = $userdata['user_gender'];
            $_SESSION['email'] = $userdata['user_email'];
            $_SESSION['oauth_provider'] = $userdata['user_oauth_provider'];             
            $_SESSION['oauth_id'] = $uid;
            $ret= true;
         }
         } else {
            # For testing purposes, if there was an error, let's kill the script
            die("There was an error.");
            $ret=false;
        }
       return $ret;
 }

 if ($client->getAccessToken()) {

 $user = $oauth2->userinfo->get();
 //print_r($user);

 $val=dooperation($user);

 header("location: home.php");

// The access token may have been updated lazily.
$_SESSION['token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
}
?>
<?php if(isset($personMarkup)): ?>
<?php print $personMarkup ?>
<?php endif ?>
<?php
   if(isset($authUrl)) {
    header("Location: $authUrl");
 }

 ?>
4

0 回答 0