如何在谷歌登录 API 中添加会话变量


how to add Session variables in google login API

我在我的codeigniter php项目中使用了google login API。身份验证有效,URL 也会重定向。但是,我想在重定向之前设置一些会话变量。

---------控制器功能----------- 功能get_html_div_login($div) { 包括谷歌 API php 库 include_once APPPATH。libraries/google-api-php-client/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;
 }

试试这个,应该已经有一个会话开始了,

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