Php

Login With Google In PHP

In this article, I’m going to Show you how to integrate login with Google in your PHP website. We have to use the Google OAuth API, which is a vary easy way to add Google login to your website .

We are using Google OAuth Version 2.0 API.

Watch Video Tutorial CLICK HERE

Setting Up the Project for Google OAuth  Authentication

Now you have to do basic setup in Google Developer Console. . which is required to integrate Google Sign In with your website .

Create a project in Google Developer Console

First of all you have to click on this link and login with your email. And if you are not registered, then you have to register here first with your Google account. You will be asked some details and some technical things will be asked about your website.

When you are signed in after registering , You home screen will look something like this. You have to click on the menu with APIs & Services on the left side .

After that you have to click on the drop-down button in the top-left  menu project.

That should open up a popup, as shown in the following screenshot.

After that you have to click on the new project button in the top-right  menu .From that you will get the form to create a new project. Like that

Here simple you have to give the name of your project, you can give whatever you want to give. And click on the create button . Now you have created a new project, now you just select the project by clicking on the SELECT PROJECT button on the right side.

After that you have to look at point 1 below and you have to select your project if your project is not selected.

Then click on the OAuth consent screen button below. And follow the image below.

Here you have to give the name of your project and provide an email, then continue.

After doing all this process, you have to click on credentials again, after that click on CREATE credential and click on OAuth client id.
In application type *
To select a web application
Then you have to name your project
Then the URL of the redirect is to be given.

After that you have to click on save and continue button and in the last you will get client id and client secret.

Google PHP SDK Client Library

Now I show you how to install the Google PHP API client library.

You can easily download library files from Composer.

If you have not installed your composer, this video is for you Click here

You can also download from here

When you download, you will see the folder structure like this.

Here you have to create a file within the root directory itself. called index.php

// init configuration
$clientID = '<YOUR_CLIENT_ID>';
$clientSecret = '<YOUR_CLIENT_SECRET>';
$redirectUri = '<REDIRECT_URI>';

Now you see the code of this file. Here you have to provide client_id in clientID variable and client_secret in clientSecret variable which we created just a while ago. And redirectUri has to provide the same URL that was given in its Google Console.

After that a file of blog.php has to be created where it will go after login successfully.


<?php

require_once 'vendor/autoload.php';
  
// init configuration
$clientID = 'client_id';
$clientSecret = 'client_secret';
$redirectUri = 'http://localhost/Google/index.php';
   
// create Client Request to access Google API
$client = new Google_Client();
$client->setClientId($clientID);
$client->setClientSecret($clientSecret);
$client->setRedirectUri($redirectUri);
$client->addScope("email");
$client->addScope("profile");
  
// authenticate code from Google OAuth Flow
if (isset($_GET['code'])) {
  $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
  $client->setAccessToken($token['access_token']);
   
  // get profile info
  $google_oauth = new Google_Service_Oauth2($client);
  $google_account_info = $google_oauth->userinfo->get();
  $email =  $google_account_info->email;
  $name =  $google_account_info->name;
  
header('Location:blog.php');
  // now you can use this profile info to create account in your website and make user logged in.
} else {
  // echo "<a href='".$client->createAuthUrl()."'>Google Login</a>";
}
?>




<!DOCTYPE html>
<html>
<head>
	<title>Google Sign In </title>

</head>
<body>
<div class="row" style="margin: 10%">
  <div class="col-md-3">
    <a class="btn btn-outline-dark" href="<?php echo $client->createAuthUrl(); ?>" role="button" style="text-transform:none">
      <img width="20px" style="margin-bottom:3px; margin-right:5px" alt="Google sign-in" src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Google_%22G%22_Logo.svg/512px-Google_%22G%22_Logo.svg.png" />
      Login with Google
    </a>
  </div>
</div>

<!-- Minified CSS and JS -->
<link   rel="stylesheet" 
        href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" 
        integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" 
        crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" 
        integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" 
        crossorigin="anonymous">
</script>

</body>
</html>

Next, you’ve to add email and profile scopes, so after login you have access to the basic profile information.

$client->addScope("email");
$client->addScope("profile");

So in this tutorial today, we saw that sign-in with Google.

If you want to see your video tutorials, then subscribe to my YouTube channel
And tell us some more topics that you want.

RECOMMENDED ARTICLES





Leave a Reply

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