PHP Web Application Authentication Using Google OAuth 2.0

This tutorial help to understand Google OAuth implementation with PHP. I will explains how PHP web applications use the Google API Client Library, and implementation of OAuth 2.0 authorization to access Google APIs. Google OAuth 2.0 allows user to share specific data with keeping their user names, passwords, and other information private.We will use Google OAuth Version 2.0 API.

Now days you can see many website are using google signin button to login user into website without registration process.Google OAuth API help to skip tedious registration process and login into website using Gmail id.Let’s start Google Oauth API with PHP.

Login with Google Account using PHP

We will let you know step by step process to register your app into Google api. The Google provides the project secrets and key on the successfully registration of app.


  • Register Web application with Google Api Dashboard.
  • Download PHP Google API

We will use following files and folder structure as follows:

  • index.php
  • google_oauth_config.php
    • vendor This folder have Google API, Google Client, Google Oauth and other libraries

You can also read other Google api Tutorials:

How to Register Project With Google Developer Console

  1. Go to the Google Developers Console and Sign-In with Google Gmail Id.
  2. Select an existing project from the projects drop-down or create a new project, For new project, We need to Enter the Project name and click Create to proceed.
  3. Select the newly created project into dropdown list and enable the Google+ API service.
  4. Select Library from the left sidebar, Now search the Google+ API service in the API list and select them and click enable.
  5. Select Credentials from sidebar, Click OAuth consent screen tab. Choose an Email address, enter the Product name and click save.
  6. Select Credentials tab, click the Create credentials drop-down and select OAuth client ID option.
  7. Now Application type section will be open, select Web application option.
  8. Enter your app origin name in authorized JavaScript origins field, App redirect Url into Authorized redirect URIs files.
  9. Finally, Click the Create button
  10. A dialog box will be open with Client ID and Client secret. This Client ID and Client secret allows you to access the Google APIs.

Configure Google OAuth into PHP application

I have used composer to download php google api client, Its Very simple just goto project path and run below command:

composer require google/apiclient:^2.0
Above command will download all libs and stored into vendor folder.

We will open google_oauth_config.php file, We will define Google Project Client ID ($clientId), Client Secret ($clientSecret), and Callback URL ($redirectURL).

View File for Login and Display Information

We will use index.php file for entry point of application, The Google Sign button will be shown when the user will come to web app, Once the authenticated with their Google account. PHP application will display user information with logout button. I am just showing success message but you can display information into page.

Logout functionality with Google OAuth2

User can able to logout from web application, We will add below code into google_oauth_config.php file,

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.