Create a Google Login Page in PHP

3 min read 1 year ago
Published on Apr 06, 2025 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, you will learn how to integrate Google login into your PHP website using the Google OAuth API. This allows users to sign in using their Google accounts, providing a seamless authentication process. We'll walk through the necessary steps, from setting up your project to allowing users to log in.

Step 1: Create Index Page

  • Start by creating an index.php file in your project directory.
  • This page will serve as the entry point for your authentication process.
  • Add a simple HTML structure and a link or button for users to click to log in with Google.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Google Login</title>
</head>
<body>
    <h1>Login with Google</h1>
    <a href="login.php">Login</a>
</body>
</html>

Step 2: Install API Package

  • Use Composer to install the Google API Client Library.
  • Run the following command in your terminal:
composer require google/apiclient
  • This package will help you interact with Google’s OAuth API easily.

Step 3: Generate Login URL

  • Create a new file named login.php.
  • In this file, you will set up the Google client and generate the login URL.
require_once 'vendor/autoload.php';

$client = new Google_Client();
$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri('http://yourdomain.com/callback.php');
$client->addScope('email');
$client->addScope('profile');

$loginUrl = $client->createAuthUrl();
header("Location: $loginUrl");
  • Replace YOUR_CLIENT_ID and YOUR_CLIENT_SECRET with your actual credentials.

Step 4: Generate Credentials

  • Go to the Google Cloud Console.
  • Create a new project, and navigate to the "Credentials" section.
  • Click on "Create credentials" and select "OAuth 2.0 Client IDs".
  • Configure the consent screen and set your redirect URI to match what's specified in your code.

Step 5: Use Credentials

  • After generating the credentials, use them in your login.php file as shown in Step 3.
  • Make sure your redirect URI is correctly set in the Google console and your code to avoid authentication errors.

Step 6: Create Redirect Page

  • Create a file named callback.php to handle the response from Google after the user authenticates.
  • Use this file to retrieve user information.
require_once 'vendor/autoload.php';

session_start();

$client = new Google_Client();
// Set your client ID and secret
$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri('http://yourdomain.com/callback.php');

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

    // Get user profile
    $oauth2 = new Google_Service_Oauth2($client);
    $userInfo = $oauth2->userinfo->get();
    echo 'User ID: ' . $userInfo->id;
    echo 'Email: ' . $userInfo->email;
}

Step 7: Authenticate with Google

  • Once users are redirected to callback.php, they will be authenticated, and their information will be displayed.
  • Store user information in your database as needed for future use.

Step 8: Remove App Permission

  • If you want to test removing permissions, go to the Google Account Security page.
  • Find your app under "Third-party apps with account access" and remove it.
  • This will allow you to test the login flow again.

Conclusion

You have successfully integrated Google login into your PHP application using the Google OAuth API. Key steps included setting up your project, generating credentials, and creating login and callback pages. Next, consider expanding your application by storing user data in a database or customizing the user experience post-login.