Login with Google Account using PHP

PHP Google OAuth API allows users to login in a web application with Google credentials. In this tutorial, you will learn how to build a Login with Google Account using PHP SDK.This is a very simple example, you can just copy-paste, and change according to your requirement.
Before starting to implement the Login with Google Account using PHP, look files structure:
  • login-with-google-account-using-php
    • vendor
    • config.php
    • index.php
    • profile.php
    • logout.php
Step 1: Login to Google Developers Console
Go to the Google API Console and Log In with your Google account.
Step 2: Select a project link
Click on the Select a project link.
Step 3: Create a New Project
Click on the New Project link and you enter the Project Name and other details.
Step 4: Create Credentials
Click on the Credentials from the left sidebar, and go to the Create Credentials screen tab and select OAuth client ID.
Step 5: OAuth for Web Application
Next, Choose the web application > type the name > enter the redirect URL > Click on the create button
Step 6: Client credentials
Click Client credentials > OAuth client ID to create a new set of credentials for our application.
Step 6:The Client id and Client secret
Finally, you have got the client id and client secret, copy them and save
Install the Google PHP SDK Client Library
Install it using Composer or install the library files manually. Click and Download
Step 7: Client Library Integration
Create file named config.php In this file, we will put the google secret key, secret id and redirect URL.
Step 8: Create file index.php
Create an index.php file. In this PHP file we will show the google login.
Step 9: Create a profile.php
Create a profile.php file. In this file, we will show logged in user information like first_name, last_name email, picture and more.
Step 9: Create a logout.php
The User click the logout button, user redirects on this file and the user session destroyed. And redirect to login.