Google Login Integration in PHP Tutorial

Introduction

In this tutorial, we will tackle about How to Integrate Google Login in PHP for your web application. The tutorial aims to provide students and new programmers a reference to learn to enhance their project security by integrating the application with one of the trusted platforms of the users such as Google. Here you will learn the step-by-step process of integrating your application with Google OAuth. Snippets and a sample working source code are also provided in this tutorial.

What is Google Login Integration?

Google Login Integration is a feature that allows your application end-users to register and log in using their Google accounts. This feature can add up to your application security. It will also help you to make your end-users trust your application and to store their data.

How to Integrate Google Login in Web Application using PHP?

First, you will need to create a new project at https://console.cloud.google.com/ and connect your application with Google OAuth Consent Screen API. To that, follow the instructions below.

1. Create a New Project in Google Cloud Console

Go to https://console.cloud.google.com/, and create a new project for your website or application.

Google Cloud Console Create Project

Next, fill in the required fields.

Google Cloud Console Create Project

Then, select your newly created project.

Google Cloud Console Create Project

2. Create an OAuth Consent Screen API

Click the menu bar located at the upper-left of the screen and click the "APIs and Services".

Google Cloud Console Create Project

Next, go to the OAuth consent screen

Google Cloud Console Create Project

Next, select the External option and click Create

Google Cloud Console Create Project

Then, fill in the required fields and click the Save and Continue button.

Google Cloud Console Create Project

After, continue to select the Save and Continue button until your reach the last step.

Google Cloud Console Create Project

2. Create a Credential

Next is to create a Credential to generate OAuth Client ID and Secret.

Google Cloud Console Create Project

Make sure to select OAuth Client ID. The client ID and Secret will be used in the coding part.

Google Cloud Console Create Project

3. Download the Google API Client SDK

Next is to download the Google API Client SDK to your project. To do that follow the instructions below.

  1. Download and Install composer @ https://getcomposer.org/
  2. Then execute the following command to download the SDK:
    • composer require google/apiclient:"^2.0"

4. Integrating the Google Login to the Site

This is the part for coding. In your login page file include the vendor/autoload.php file to load the Google API Client Library. Then follow the following snippet.

  1. <?php
  2. require_once('vendor/autoload.php');
  3. $clientID = "ClientID";
  4. $secret = "ClientSecret";
  5.  
  6. // Google API Client
  7. $gclient = new Google_Client();
  8.  
  9. // Set the ClientID
  10. $gclient->setClientId($clientID);
  11. // Set the ClientSecret
  12. $gclient->setClientSecret($secret);
  13. // Set the Redirect URL after successful Login
  14. $gclient->setRedirectUri('http://localhost/myapp/login.php');
  15.  
  16. // Adding the Scopr
  17. $gclient->addScope('email');
  18. $gclient->addScope('profile');

And for your login button, you can simply create a button or anchor tag element and set the URL to goto using createAuthUrl() of the Google_Client class. Here's the following example:

  1. <a href="<?= $gclient->createAuthUrl() ?>" class="btn btn btn-primary btn-flat rounded-0">Login with Google</a>

To handle the code after the successful login at Google, you can use the following snippet or script to get the data or profile of the logged user.

  1. if(isset($_GET['code'])){
  2. // Get Token
  3. $token = $gclient->fetchAccessTokenWithAuthCode($_GET['code']);
  4.  
  5. // Check if fetching token did not return any errors
  6. if(!isset($token['error'])){
  7. // Setting Access token
  8. $gclient->setAccessToken($token['access_token']);
  9.  
  10. // store access token
  11. $_SESSION['access_token'] = $token['access_token'];
  12.  
  13. // Get Account Profile using Google Service
  14. $gservice = new Google_Service_Oauth2($gclient);
  15.  
  16. // Get User Data
  17. $udata = $gservice->userinfo->get();
  18. }
  19. }

That's it! You now create a script to register and login the user to your web application. You can build your registration and login logic by saving some of the user data. Here's an example full script for handling the data.

  1. <?php
  2.  
  3. require_once('vendor/autoload.php');
  4.  
  5. $clientID = "ClientID";
  6. $secret = "ClientSecret";
  7.  
  8. // Google API Client
  9. $gclient = new Google_Client();
  10.  
  11. $gclient->setClientId($clientID);
  12. $gclient->setClientSecret($secret);
  13. $gclient->setRedirectUri('http://localhost/test_login/login.php');
  14.  
  15.  
  16. $gclient->addScope('email');
  17. $gclient->addScope('profile');
  18.  
  19. if(isset($_GET['code'])){
  20. // Get Token
  21. $token = $gclient->fetchAccessTokenWithAuthCode($_GET['code']);
  22.  
  23. // Check if fetching token did not return any errors
  24. if(!isset($token['error'])){
  25. // Setting Access token
  26. $gclient->setAccessToken($token['access_token']);
  27.  
  28. // store access token
  29. $_SESSION['access_token'] = $token['access_token'];
  30.  
  31. // Get Account Profile using Google Service
  32. $gservice = new Google_Service_Oauth2($gclient);
  33.  
  34. // Get User Data
  35. $udata = $gservice->userinfo->get();
  36. foreach($udata as $k => $v){
  37. $_SESSION['login_'.$k] = $v;
  38. }
  39. $_SESSION['ucode'] = $_GET['code'];
  40.  
  41. header('location: ./');
  42. }
  43. }
  44. echo '<a href="'.$gclient->createAuthUrl().'" class="btn btn btn-primary btn-flat rounded-0">Login with Google</a>';
  45. ?>

DEMO VIDEO

That's the end of this tutorial. I hope this tutorial helps you with what you are looking for and that you'll find this useful for your current and future PHP projects. I created a simple application that demonstrates the Google Login Integration in PHP for this tutorial. You can download it on this site for free. The download button is located below. Due to the size limitation for uploading the source code file on this site, I didn't include the SDK files but still, you can download it by following Step #3 of this tutorial. Also, read the install_instruction.txt to read some further instructions about how to run the application properly.

Explore this website for more Tutorials and Free Source Codes.

Happy Coding :)

Comments

Submitted byPorcino888 (not verified)on Thu, 05/11/2023 - 01:16

THANK YOU SO MUCH

I was looking for tutorials on php, this is so much better than having to endure some dull lectures at class. Thanks, this webpage is awesome! :'D

Also, I needed to relearn how to use OAuth again, and this was a lifesaver

Add new comment