将php从7升级到7.2后,由Google进行身份验证失败

时间:2018-09-27 13:34:41

标签: php codeigniter google-oauth php-7.2

我确实将我的php版本从php 7升级到php 7.2.10

我有一个使用codeigniter编写的网站,在此应用程序的某些部分中,我使用了 Google_Service_Oauth2

使用以下消息升级php版本后,由Google进行身份验证的请求失败:

  

页面重定向不正确

     

Firefox已检测到服务器正在重定向请求   这个地址永远无法完成。

This problem can sometimes be caused by disabling or refusing to accept cookies.

这是我的功能:

function byGoogle()
{
    require_once ('global/google/libraries/Google/autoload.php');

    ########## Google Settings.Client ID, Client Secret from https://console.developers.google.com #############
    $client_id = 'my-client-id-part.apps.googleusercontent.com'; 
    $client_secret = '_My_secret-Qx01';
    $redirect_uri = 'http://codepen.ir/login/byGoogle';


    ########## MySql details  #############
    $db_username = "username"; //Database Username
    $db_password = "pass"; //Database Password
    $host_name = "localhost"; //Mysql Hostname
    $db_name = 'dbName'; //Database Name
    ###################################################################

    $client = new Google_Client();
    $client->setClientId($client_id);
    $client->setClientSecret($client_secret);
    $client->setRedirectUri($redirect_uri);
    $client->addScope("email");
    $client->addScope("profile");

    $service = new Google_Service_Oauth2($client);

    //If $_GET['code'] is empty, redirect user to google authentication page for code.
    //Code is required to aquire Access Token from google
    //Once we have access token, assign token to session variable
    //and we can redirect user back to page and login.
    if (isset($_GET['code'])) {
      $client->authenticate($_GET['code']);
      $_SESSION['access_token'] = $client->getAccessToken();
      header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
      exit;
    }

    //if we have access_token continue, or else get login URL for user
    if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
      $client->setAccessToken($_SESSION['access_token']);
    } else {
      $authUrl = $client->createAuthUrl();
    }

    //Display user info or display login url as per the info we have.
    //echo '<div style="margin:20px">';
    if (isset($authUrl)){ 

        redirect( $authUrl );
        exit();
    } else {

        $user = $service->userinfo->get(); //get user info 



        $user_info = $this->md_login->getUserByMail($user->email);

        if($user_info === FALSE)
        {
             //some DB stuff

            if( $register )
            {
                $_SESSION['email']  = $email;
                $_SESSION['login']  = TRUE;
                $_SESSION['access'] = $user_type;

                if( $this->session->has_userdata('referer'))
                {
                    redirect( $this->session->userdata('referer'));
                }//if referer
                else
                {
                    redirect( base_url() );
                }//no referer

            }


        }//new user
        else
        {
            $_SESSION['email']  = $user->email;
            $_SESSION['login']  = true;
            $_SESSION['access'] = $user_info['user_type'];

            if( $this->session->has_userdata('referer'))
            {
                redirect( $this->session->userdata('referer'));
            }//if referer
            else
            {
                redirect( base_url() );
            }//no referer
        }

    }

}//byGoogle

在此php版本更改之前,代码运行良好。

有人为什么会这样吗?

预先感谢

0 个答案:

没有答案