Login with Google account using PHP

Google oAuth API allow web developer to integrate login system at the website by the quick, easy and powerful way. Google login system helps to increase your website registrations. Because most of the users have a google account and they can easily login at your website using their google account. We have implemented a simple login system using Google API PHP library and make the google authentication simple.

This tutorial explains how to implement login with Google account using PHP and store the user information to the MySQL database. Also, we’ll discuss how can you integrate Login with Google Account script at your web project. From the Demo link you can view the live demo of Google login system. Also you can download the full source code of Login with Google using PHP from the Download link. Just follow the below tutorial for easily implementing our simple Google login script at your website within a minute. Let’s start login with google using PHP and MySQL tutorial.


This Google login project contains three folders named src/, includes/ and images/ with some PHP files. The folders and files structure are given below:

  • src/
    • Google API, Google Client, Google Oauth and other libraries
  • includes/
    • functions.php
  • config.php
  • index.php
  • account.php
  • logout.php
  • images/

Google Project Creation:

 Go to the Google Developers Console – https://console.developers.google.com/
 Select an existing project, or create a new project by clicking Create Project:

  • In the Project name field, type in a name for your new project.
  • In the Project ID field, the console has created project ID. Optionally you can type in a project ID for your project. But project ID must be unique world-wide.
  • Click on the Create button and the project to be created within some seconds. Once the project is created successfully, the project name would be appearing at the top of the left sidebar.

 In the left sidebar, select APIs under the APIs & auth section. A list of Google APIs appears.

 Find the Google+ API service and set its status to Enable.

 In the sidebar, select Credentials under the APIs & auth section.

 In the OAuth section of the page, select Create New Client ID.

  • Create Client ID dialog box would be appearing for choosing application type.
  • In the Application type section of the dialog, select Web application and click on the Configure consent screen button.
  • Choose Email address, enter Product name and save the form.
  • In the Authorized JavaScript origins field, enter your app origin. If you want to allow your app to run on different protocols, domains, or subdomains, then you can enter multiple origins.
  • In the Authorized redirect URI field, enter the redirect URL.
  • Click on Create Client ID.

 Now you can see the Client ID for web application section. Note the Client ID and Client secret that will need to use to access the APIs.


Database table creation:

To store the user information, you need to create a database and a table. At first create a database like codexworld. After that copy the below SQL query and run the SQL query on the database.

CREATE TABLE `users` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `oauth_provider` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `oauth_uid` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `fname` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `lname` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `gender` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
    `locale` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
    `gpluslink` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `picture` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `created` datetime NOT NULL,
    `modified` datetime NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


In this project you just have to modify only two files.
 Database configuration (includes/functions.php):functions.php file have Users class with some functions. Into the __construct() function we have configured the database and connect with the database. You need only to change the $dbServer, $dbUsername, $dbPassword and $dbName variables value with your MySQL server details.

function __construct(){
//database configuration
$dbServer 'localhost'//Define database server host
$dbUsername 'root'//Define database username
$dbPassword ''//Define database password
$dbName 'codexworld'//Define database name
    //connect databse
$con mysqli_connect($dbServer,$dbUsername,$dbPassword,$dbName);
"Failed to connect with MySQL: ".mysqli_connect_error());
$this->connect $con;

 Google API configuration (config.php): Open the config.php file and modify the $clientId, $clientSecret, $redirectUrl and $homeUrl variables value.
Note that: You can find the $clientId and $clientSecret at the Google project page.(Go to the https://console.developers.google.com/ => click on your project => click on the “APIs & auth” link from the left menu panel => click on the Credentials link from under the APIs & auth menu => copy the Client ID and Client Secret)


 * Google Project API Credentials
$clientId 'Google Project Client ID'//Google CLIENT ID
$clientSecret 'Google Project Client Secret'//Google CLIENT SECRET
$redirectUrl 'http://localhost/login-with-google-using-php';  //return url (url to script)
$homeUrl http://localhost/login-with-google-using-php';  //return to home

 * Google Client Configuration
$gClient = new Google_Client();
$gClient->setApplicationName('Login to codexworld.com');

$google_oauthV2 = new Google_Oauth2Service($gClient);

 If you have any query about this tutorial and scripts, feel free to comment here.


  1. Bill Said...
  2. Vinay Said...
  3. Ebenezer Said...
  4. Abhay Suresh Bhosale Said...
  5. Tom Isenbaert Said...
  6. Pritesh Said...
    • CodexWorld Said...

Leave a reply

Connect With CodexWorld