Login with Google Account using PHP

Login with Google account is useful feature for any community website and other type of website. Today we implement login with Google account step by step.

Login with Google Account using PHP

Here we first get know the complete steps then we elaborate the process of implementation.

Go below to download the complete source code of Login with Google Account using PHP

Complete steps to implement Login with Google Account using PHP

  1. Create Google API credentials.
  2. Download Google API Client Library for PHP.
  3. Create Google Config PHP file
  4. Create an index PHP file
  5. Create logout

Steps to Create Google API credentials

  1. Go to https://console.developers.google.com/ Google developer console by the given link.

2. Click on create project like shown on image below,

login with google account
login with google
Create Project

3. After that, if you have already multiple projects then select one which you create at a time.

login using google in php
Select Project

4. Click on the OAuth option which is available on the sidebar of the Google developer console.

login with google
Go to OAuth Screen

5. Now we create OAuth Client ID and secret ID by filling the OAuth form like below and hit the Save button which is available on the very bottom of the page.

Google Oauth setup
Google OAUth Setup

6. After the above steps, we got the Client ID and Clent secret by creating credentials steps,
Click on CREATE CREDENTIALS

Go to OAuth Client ID
Go to Craete Credentials

7. Select Web Application from the dropdown

Craete Auth ID google
Craete Auth ID google

8. After that fille the form put the application name and redirection UEL (After successful logout where the page goes to).

Fill Auth credentials form
Fill Auth credentials form

9. Now you get your client ID and secret ID

Clent ID and secret
Oauth Client ID and secret

Google API Client Library for PHP

Above are the complete steps to creating Google OAuth client ID and secret ID. Now we set up Google API Client Library for PHP by using composer and CLI or Git Bash.

If you do not know how to install composer on the system check here:
Install Composer on Linux and Windows in 3 Steps

Run the below command on your CLI or Git Bash to set up or download the Google API client PHP library,

composer require google/apiclient:"^2.0"

After that we setup the config file where we set the below things,

  1. Set the OAuth 2.0 Client ID
  2. Set the OAuth 2.0 Client Secret key
  3. Set the OAuth 2.0 Redirect URI

config.php File

On above we also setup session and mentioned which fields are we get after Google login.

Now we create our index file where we put our design and get the user data and store it to session and print on the web page. Also, set up the button for login with Google.

index.php File

All the line by line explainations are commented on the source code.

Now we create logout file where we revoke the session data.

logout.php File

Here is the complete source code of Login with Google Account using PHP.

Download Complete source code of Login with Google Account using PHP,

[sociallocker][wpdm_package id=’2546′] [/sociallocker]

Also Check:

Happy Coding..!

Was this article helpful?
YesNo

By Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.

19 thoughts on “Login with Google Account using PHP Step by Step”
  1. Working Perfectly with localhost but not working on live domain. Actually I’m using this with many domains but in 1 of my domain it is not working with no error. please help

Leave a Reply

Your email address will not be published. Required fields are marked *