Skip to content
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Login with Google Account using PHP

Login with Google Account using PHP

Posted on June 7, 2018September 26, 2021 By XpertPhp No Comments on Login with Google Account using PHP

In this article, We will explain how to create login with google account using PHP. Here we will create a system where the user can log in with his Google account on your website. So, in this post, we share a tutorial on how to login or sign in or sign up with a Google account in your PHP web application.

To login with Google account using PHP, Google has provided Google OAuth API, which is very easy and useful to implement login using Google account on your website. Google sign in API provides rights to the user to sign in to the website using their Google account credential without registering on that particular website.

In this example, we will use Google OAuth API version 2.0 (Google OAuth Sign In API), this API gives users permission to sign in with their existing Google accounts on your website. Below, you can find how to integrate Google login API into your PHP website and you can also find the process of how to get the API key and how to integrate Google client library into your existing PHP library to login.

Create Google OAuth Credentials

Now we need to Client ID and Client Secret key for creating google sign in. So let’s first go to the Google developer console page and then follow the screenshot to generate the id and key.

google_client_id_secret

Download / Install PHP Google API Client Library

After we got the Google API key, now we need to download the Google API client library for PHP. For this, we need to go to the command prompt and first type the command composer, after that, we need to run the following command.

1
composer require google/apiclient:"^2.0"

PHP Google OAuth Authentication Code

config.php
now, first we need to create config.php file. after that, we will configure client id and secret key and load google api client library in this file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
 
require_once 'vendor/autoload.php';
 
$google_client = new Google_Client();
 
$google_client->setClientId('here enter your google client Id');
 
$google_client->setClientSecret('here enter you google client secret key');
 
$google_client->setRedirectUri('http://localhost/php_google_login/');
 
$google_client->addScope('email');
 
$google_client->addScope('profile');
 
session_start();
 
?>

index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
include('config.php');
 
$login_button = '';
 
if(isset($_GET["code"]))
{
$token = $google_client->fetchAccessTokenWithAuthCode($_GET["code"]);
 
if(!isset($token['error']))
{
  $google_client->setAccessToken($token['access_token']);
 
  $_SESSION['access_token'] = $token['access_token'];
 
  $google_service = new Google_Service_Oauth2($google_client);
 
  $data = $google_service->userinfo->get();
 
  if(!empty($data['given_name']))
  {
   $_SESSION['user_first_name'] = $data['given_name'];
  }
 
  if(!empty($data['family_name']))
  {
   $_SESSION['user_last_name'] = $data['family_name'];
  }
 
  if(!empty($data['email']))
  {
   $_SESSION['user_email_address'] = $data['email'];
  }
 
  if(!empty($data['gender']))
  {
   $_SESSION['user_gender'] = $data['gender'];
  }
 
  if(!empty($data['picture']))
  {
   $_SESSION['user_image'] = $data['picture'];
  }
}
}
 
if(!isset($_SESSION['access_token']))
{
$login_button = '<a href="'.$google_client->createAuthUrl().'"><img src="google-signin-buttons.png" /></a>';
}
 
?>
<html>
<head>
  <title>Login with Google Account using PHP</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2 align="center">Login with Google Account using PHP</h2>
<div class="panel panel-default">
   <?php
   if($login_button == '')
   {
echo '<div class="panel-heading">Welcome User</div><div class="panel-body">';
echo '<img src="'.$_SESSION["user_image"].'" class="img-responsive img-circle img-thumbnail" />';
echo '<h3><b>Name :</b> '.$_SESSION['user_first_name'].' '.$_SESSION['user_last_name'].'</h3>';
echo '<h3><b>Email :</b> '.$_SESSION['user_email_address'].'</h3>';
echo '<h3><a href="logout.php">Logout</h3></div>';
   }
   else
   {
echo '<div class="panel-body" align="center">'.$login_button . '</div>';
   }
   ?>
</div>
</div>
</body>
</html>

delete.php

1
2
3
4
5
6
7
8
9
10
11
<?php
// destroy all session
include('config.php');
 
$google_client->revokeToken();
 
session_destroy();
 
header('location:index.php');
 
?>

PHP Google OAuth Login Source Code

Please follow and like us:
error
fb-share-icon
Tweet
fb-share-icon

Recommended Posts:

  • How to create custom captcha using php with ajax
  • How to Send Email with Attachment in PHP
  • PHP CRUD operation with MySQL Example
  • CRUD Operation using JSON File in PHP
  • show multiple checkbox checked with multiple array using php
Php Tags:Login With Google in php

Post navigation

Previous Post: CodeIgniter join query example
Next Post: Codeigniter Single and Multiple records Insert Query

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tricks jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch Laravel Socialite laravel social login nodejs pagination payment gateway php with mysql react js tutorial rewrite rule send mail validation wysiwyg editor

Latest Posts

  • How to Convert Date and Time from one timezone to another in php
  • how to get current date and time in php
  • Drag and Drop Reorder Items with jQuery, PHP & MySQL
  • Laravel 9 Toastr Notifications Example Tutorial
  • Laravel 9 CRUD Operation Example Using Google Firebase
  • Laravel 9 CKeditor Image Upload With Example
  • Laravel 9 Summernote Image Upload With Example
  • Laravel 9 Stripe Payment Gateway Integrate Example
  • How To Send Email Using Mailtrap In Laravel 9
  • Laravel 9 Fullcalendar Ajax Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Age Calculator Online
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Copyright © 2018 - 2022,

All Rights Reserved Powered by XpertPhp.com