Implementing Google account authentication is very popular these days. ⁤⁤It is easy and powerful method for user authentication, Google account authentication takes less effort from user for registration or login on your system. ⁤⁤there is no need for the user to remember the username and password.

PHP Google Login - Implement  Google Account Login in Php Application

Table Of Content

1 Prerequisites

1.) PHP version of >7.4
2.) Composer
3.) Google Account

2 Introduction

In this article, We are going to see how to implementation Google account authentication to your PHP application. We’ll be using Google Client Library.

3 Create Project Folder "google-signin-app"

Create Project Folder "google-signin-app" in root directory store all project files

4 Install Google Client Library

First, make sure your computer has a composer.
Use the following command to install Google Client Library via Composer.

composer require google/apiclient:^2.0

This command will download the google api client Library and add it to your project.

5 Create Google API Console Project

5.1 Login into Google Developer Account

If you have Google Developer Account Go to https://console.cloud.google.com/, else you can easily create one directly from the Google Developers Console.

5.2 Create New Project

Create a Project in the Google Developer Console

Create Project

Entering Project information in the following Screen

Project Details

Project successfully created with given information's

Project Created Successfully

5.3 Create Credentials

Now Create the credential by choosing "Credentials" in the side bar, click "Create Credentials" button and choose "OAuth Client ID".

Create Credentials

Now Click "Configure Consent Screen" Button.

Configure Consent Screen

It will redirected to the following screen and choose "External" option then create.

external

5.5 Update App Information

By providing App and Developer Information Complete the form then click "Save and Continue" Button.

App Information

5.6 OAuth Client Created

Now Create the OAuth Client by choosing "Credentials" in the side bar, click "Create Credentials" button and choose "OAuth Client ID".

Create Credentials

It will redirected to the following screen and fill the detail about our app and fill the authorized redirect URIs. This is the URI that we will use to redirect user after they choose their Google account to login to our web. For example here I use http://localhost/google-signin-app/google-callback.php for the callback URI..

Create Oauth

Now we get the Client ID and the Client Secret.

Oauth Successfully

6 Configure Google App Credentials(config.php)

Insert the Client ID and Client Secret key and redirect URI into the config.php file, Which we obtained from previous step GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET. Additionally, include a new key in the config.php file called GOOGLE_REDIRECT_URI and populate it with the callback URI used in the Google API Console.

define('GOOGLE_CLIENT_ID', 'Your Client ID');   
define('GOOGLE_CLIENT_SECRET', 'Your Client Secret');   
define('GOOGLE_REDIRECT_URI', 'http://localhost/google-signin-app/google-callback.php');   

7 Create Login Page - login.php

Create a simple login page (login.php) that includes a "Login with Google" button: google-signin-app/login.php

<?php
include('config.php');
require_once 'vendor/autoload.php';

$client = new Google_Client();
$client->setClientId(GOOGLE_CLIENT_ID);
$client->setClientSecret(GOOGLE_CLIENT_SECRET);
$client->setRedirectUri(GOOGLE_REDIRECT_URI);
$client->addScope('email');
$client->addScope('profile');

$login_url = $client->createAuthUrl();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login with Google</title>
</head>
<body>
    <h1>Login with Google</h1>
    <a href="<?= htmlspecialchars($login_url) ?>">Login with Google</a>
</body>
</html>

8 Create Google Callback Page - google-callback.php

Create a file (google-callback.php) to handle the callback from Google after authentication google-signin-app/google-callback.php

<?php
require_once 'vendor/autoload.php';
session_start();
include('config.php');

$client = new Google_Client();
$client->setClientId(GOOGLE_CLIENT_ID);
$client->setClientSecret(GOOGLE_CLIENT_SECRET);
$client->setRedirectUri(GOOGLE_REDIRECT_URI);
$client->addScope('email');
$client->addScope('profile');

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

    // Get user info
    $oauth2 = new Google_Service_Oauth2($client);
    $userInfo = $oauth2->userinfo->get();

    // Store user info in session or database
    $_SESSION['id'] = $userInfo->id;
    $_SESSION['email'] = $userInfo->email;
    $_SESSION['name'] = $userInfo->name;

    // Redirect to a secured page or dashboard
    header('Location: dashboard.php');
    exit();
}

9 Create Dashboard - dashboard.php

Create a simple secured page (dashboard.php) that only logged-in users can access: google-signin-app/dashboard.php

<?php
session_start();
if (!isset($_SESSION['id'])) {
    header('Location: login.php');
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Dashboard</title>
</head>
<body>
    <h5>Google Account Details</h5>
    <p>Google ID: <?= htmlspecialchars($_SESSION['id']); ?></p>
    <p>Name: <?= htmlspecialchars($_SESSION['name']); ?></p>
    <p>Your email: <?= htmlspecialchars($_SESSION['email']); ?></p>
    <p><a href="logout.php">Logout</a></p>
</body>
</html>

10 Implement Logout Functionality

Create a logout page (logout.php) to destroy the session and log the user out: google-signin-app/logout.php

<?php
session_start();
session_destroy();
header('Location: login.php');
exit();

11 Folder Structure

12 Run Web Server to Test the App

Visit the URL http://localhost/google-signin-app/login.php

13 Conclusion

That’s all we need to do.
This guide should help you integrate Google Login in your PHP application

Tags