XpertPhp

  • 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

Login with Google Account using PHP

June 7, 2018 XpertPhp Comments 0 Comment

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');
 
?>

Read Also

PHP Login And Registration Example

Login And Registration Example Using Pdo

PHP Google OAuth Login Source Code

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

Php
Login With Google in php

Post navigation

NEXT
Codeigniter Single and Multiple records Insert Query
PREVIOUS
CodeIgniter join query example

Latest Posts

  • Laravel 10 Dropzone Image Upload Example Tutorial
  • Laravel 10 Fullcalendar Example Tutorial
  • Laravel 10 Form Validation Example Tutorial
  • Laravel 10 Image Upload Example Tutorial
  • Laravel 10 Pagination Example Tutorial
  • Laravel 10 Send Email Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JQuery Interview Questions and Answers

SUBSCRIBE TO OUR NEWSLETTER

Email


Categories

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

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

Sponsored Links : minifytools.com

© Copyright 2018 - © 2023, All Rights Reserved Powered by XpertPhp.com