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.
Next, fill in the required fields.
Then, select your newly created 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".
Next, go to the OAuth consent screen
Next, select the External option and click Create
Then, fill in the required fields and click the Save and Continue button.
After, continue to select the Save and Continue button until your reach the last step.
2. Create a Credential
Next is to create a Credential to generate OAuth Client ID and Secret.
Make sure to select OAuth Client ID. The client ID and Secret will be used in the coding part.
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.
- Download and Install composer @ https://getcomposer.org/
- 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.
- <?php
- require_once('vendor/autoload.php');
- $clientID = "ClientID";
- $secret = "ClientSecret";
- // Google API Client
- $gclient = new Google_Client();
- // Set the ClientID
- $gclient->setClientId($clientID);
- // Set the ClientSecret
- $gclient->setClientSecret($secret);
- // Set the Redirect URL after successful Login
- $gclient->setRedirectUri('http://localhost/myapp/login.php');
- // Adding the Scopr
- $gclient->addScope('email');
- $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:
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.
- // Get Token
- $token = $gclient->fetchAccessTokenWithAuthCode($_GET['code']);
- // Check if fetching token did not return any errors
- // Setting Access token
- $gclient->setAccessToken($token['access_token']);
- // store access token
- $_SESSION['access_token'] = $token['access_token'];
- // Get Account Profile using Google Service
- $gservice = new Google_Service_Oauth2($gclient);
- // Get User Data
- $udata = $gservice->userinfo->get();
- }
- }
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.
- <?php
- require_once('vendor/autoload.php');
- $clientID = "ClientID";
- $secret = "ClientSecret";
- // Google API Client
- $gclient = new Google_Client();
- $gclient->setClientId($clientID);
- $gclient->setClientSecret($secret);
- $gclient->setRedirectUri('http://localhost/test_login/login.php');
- $gclient->addScope('email');
- $gclient->addScope('profile');
- // Get Token
- $token = $gclient->fetchAccessTokenWithAuthCode($_GET['code']);
- // Check if fetching token did not return any errors
- // Setting Access token
- $gclient->setAccessToken($token['access_token']);
- // store access token
- $_SESSION['access_token'] = $token['access_token'];
- // Get Account Profile using Google Service
- $gservice = new Google_Service_Oauth2($gclient);
- // Get User Data
- $udata = $gservice->userinfo->get();
- foreach($udata as $k => $v){
- $_SESSION['login_'.$k] = $v;
- }
- $_SESSION['ucode'] = $_GET['code'];
- exit;
- }
- }
- echo '<a href="'.$gclient->createAuthUrl().'" class="btn btn btn-primary btn-flat rounded-0">Login with Google</a>';
- ?>
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
Add new comment
- Add new comment
- 4877 views