2016-03-14 29 views
0

我在我的codeigniter php項目中使用了Google登錄API。身份驗證有效並且URL也被重定向。但是,我想在重定向之前設置一些會話變量。如何在Google登錄API中添加會話變量

// ---------控制器功能----------- 功能get_html_div_login($ DIV) {// 包括谷歌的API PHP庫 include_once APPPATH。 「庫/谷歌-API的PHP的客戶端/ Google_Client.php」; include_once APPPATH。「libraries/google-api-php-client/contrib/Google_Oauth2Service.php」;

 // Google Project API Credentials 
     $clientId = '###  removed by Billy, DO NOT INSERT PROPER CREDENTIALS'; 
     $clientSecret = '###  removed by Billy, DO NOT INSERT PROPER CREDENTIALS'; 
     $redirectUrl = base_url() . 'user_authentication/'; 

     // Google Client Configuration 
     $gClient = new Google_Client(); 
     $gClient->setApplicationName('hashtag'); 
     $gClient->setClientId($clientId); 
     $gClient->setClientSecret($clientSecret); 
     $gClient->setRedirectUri($redirectUrl); 
     $google_oauthV2 = new Google_Oauth2Service($gClient); 

     if (isset($_REQUEST['code'])) { 
      $gClient->authenticate(); 
      $this->session->set_userdata('token',$gClient->getAccessToken()); 
      redirect($redirectUrl); 
     } 

     $token = $this->session->userdata('token'); 
     if (!empty($token)) { 
      $gClient->setAccessToken($token); 
     } 

     if ($gClient->getAccessToken()) { 
      $userProfile = $google_oauthV2->userinfo->get(); 
      // Preparing data for database insertion 
      $userData['oauth_provider'] = 'google'; 
      $userData['oauth_uid'] = $userProfile['id']; 
      $userData['first_name'] = $userProfile['given_name']; 
      $userData['last_name'] = $userProfile['family_name']; 
      $userData['email'] = $userProfile['email']; 
      $userData['gender'] = $userProfile['gender']; 
      $userData['locale'] = $userProfile['locale']; 
      $userData['profile_url'] = $userProfile['link']; 
      $userData['picture_url'] = $userProfile['picture']; 
      // Insert or update user data 
      $userID = $this->user->checkUser($userData); 
      if(!empty($userID)){ 
       $data['userData'] = $userData; 
       $this->session->set_userdata('userData',$userData); 
      } else { 
       $data['userData'] = array(); 
      } 
     } else { 
      $data['authUrl'] = $gClient->createAuthUrl(); 
     } 
     // $this->load->view('user_authentication/index',$data); 
     echo $this->load->view($div,$data); 
} 

// -------- MODEL FUNCTION -------------- 
public function checkUser($data = array()){ 
     $this->db->select($this->primaryKey); 
     $this->db->from($this->tableName); 
     $this->db->where(array('oauth_provider'=>$data['oauth_provider'],'oauth_uid'=>$data['oauth_uid'])); 
     $prevQuery = $this->db->get(); 
     $prevCheck = $prevQuery->num_rows(); 

     if($prevCheck > 0){ 
      $prevResult = $prevQuery->row_array(); 
      $data['modified'] = date("Y-m-d H:i:s"); 
      $update = $this->db->update($this->tableName,$data,array('id'=>$prevResult['id'])); 
      $userID = $prevResult['id']; 
     }else{ 
      $data['created'] = date("Y-m-d H:i:s"); 
      $data['modified'] = date("Y-m-d H:i:s"); 
      $insert = $this->db->insert($this->tableName,$data); 
      $userID = $this->db->insert_id(); 
     } 

     return $userID?$userID:FALSE; 
} 

回答

1

試試這個,應該有一個會議已經開始,

if (isset($_REQUEST['code'])) { 
 
      $gClient->authenticate(); 
 
      $this->session->set_userdata('token',$gClient->getAccessToken()); 
 
$_SESSION['var1']="hello"; 
 
$_SESSION['var2']="there"; 
 
      redirect($redirectUrl);