DEV Community

Hòa Nguyễn Coder
Hòa Nguyễn Coder

Posted on

Login Google Using PHP

Login with google, we can create Google API Console from google
login google using php
login google using php
login google using php

After then, We have Client ID & Client Secret, begin download googleapis or install from composer

$composer require google/apiclient:"^2.0"
Enter fullscreen mode Exit fullscreen mode

or

Download Googleapis

Continue, we create file define.php in http://localhost/LoginGoogle/define.php

//define.php
<?php
    //set define login google
    define('GOOGLE_APP_ID','8330207686-ih6vo9kvhqqfnr.apps.googleusercontent.com');
    define('GOOGLE_APP_SECRET','vHI3K4N5obLHgz');
    define('GOOGLE_APP_CALLBACK_URL','http://localhost/LoginGoogle/redirect-google.php');
    define('LOCALHOST','localhost');
    define('USERNAME','root');
    define('PASSWORD','');
    define('DATABASE','ABC');
?>
Enter fullscreen mode Exit fullscreen mode

Create File redirect-google.php in http://localhost:LoginGoogle/redirect-google.php

<?php
    require_once('define.php');

    /**
     * SET CONNECT 
     */
    $conn = mysqli_connect(LOCALHOST,USERNAME,PASSWORD,DATABASE);
    if (!$conn) {
        echo "Error: Unable to connect to MySQL." . PHP_EOL;
        echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
        echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
        exit;
    }


    /**
     * CALL GOOGLE API
     */
    require_once 'google-api-php-client-2.4.0/vendor/autoload.php';
    $client = new Google_Client();
    $client->setClientId(GOOGLE_APP_ID);
    $client->setClientSecret(GOOGLE_APP_SECRET);
    $client->setRedirectUri(GOOGLE_APP_CALLBACK_URL);
    $client->addScope("email");
    $client->addScope("profile");

    if (isset($_GET['code'])) {
        $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
       // print_r($token);
        $client->setAccessToken($token['access_token']);

        // get profile info
        $google_oauth = new Google_Service_Oauth2($client);
        $google_account_info = $google_oauth->userinfo->get();
        $email =  $google_account_info->email;
        $name =  $google_account_info->name;
       // print_r($google_account_info);
       /**
        * CHECK EMAIL AND NAME IN DATABASE
        */
        $check = "SELECT * FROM `users` WHERE `email`='".$email."' and `name`='".$name."'";
        $result = mysqli_query($conn,$sql);
        $rowcount=mysqli_num_rows($result);
        if($rowcount>0){
            /**
             * USER EXITS
             */
            header('location:home');
        }
        else{
            /**
             * INSERT USER TO DATABASE
             * AFTER INSERT, YOU CAN HEADER TO HOME
             */

        }

    } else {
        /**
         * IF YOU DON'T LOGIN GOOGLE
         * YOU CAN SEEN AGAIN GOOGLE_APP_ID, GOOGLE_APP_SECRET, GOOGLE_APP_CALLBACK_URL
         */
        echo "<a href='".$client->createAuthUrl()."'>Google Login</a>";
    }
?>
Enter fullscreen mode Exit fullscreen mode

After then you can open http://localhost/LoginGoogle/redirect-google.php
login google using php

Post:Login Google using PHP

Top comments (1)

Collapse
 
ebojorq profile image
Efraín Bojórquez García

I'm using PHP version 8.3.7 in my localhost environment. My web server has 8.3.2 installed (newest available)

When I try and run this in my localhost, it runs smoothly, no issues at all.

When I run this in my production environment, it gets stuck here:

$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);

I do get a $_GET['code'], but, for some reason this is where it gets stuck...

My localhost runs with HTTP, my production with HTTPS... could it be something in the certificate that is messing up the redirection?