Login with Google Account using PHP

Login with Google Account using PHP

 

Welcome folks I am back with another blog post. In this post we will be implementing How to Login with Google Account using PHP or in other words implement Google Auth in PHP . For this application we first of all need to create a project in Google Developers Console and get your client id and client secret. This process is illustrated in the next steps in a detailed fashion.

 

 

So just create a new project by providing a certain name or you can select from the created projects.

 

 

After selecting the project just click on create credentials and create a brand new OAuth Client ID.

 

 

 

Just select the option of web application because we are creating a web application.

 

So in this just provide localhost for the first field and the second field is important it needs to be the same for your project you can have different redirect url. It is basically the url to which Google redirects you whenever the user grants access to your application. Select it cautiously it needs to be same for your project.

 

 

Click on the create button to generate the client id and client secret. It will be different for you and don’t share this with others. Just copy both and store it somewhere we will be using it later in the application.

READ  Implement Google Auth in PHP using cURL Http Request

 

 

For the sake of simplicity just download the json file for your client id which will contain all the information for you. Just download it and include it in your php file.

 

 

 

Download PHP OAuth Client Library

 

After that you need to download a php OAuth Client library which lets you connect with the Google Account and fetches the information for you in the background

 

 

After you download you will get a zip file shown below

 

 

Extract the zip file and you will get the folder something like this

 

Just open text editor make a new file config.php file and copy paste the following code into it

 

<?php

require_once 'phpclient/vendor/autoload.php';
require_once 'client.json';

session_start();

$client = new Google_Client();

$client->setApplicationName("Login with Google Account using PHP");

$client->setAuthConfig("client.json");

$client->addScope([Google_Service_Oauth2::PLUS_LOGIN,Google_Service_Oauth2::USERINFO_EMAIL]);

$client->setRedirectUri("http://localhost/googleauth/redirect.php");



?>

In this configuration php script we are actually importing the libraries which we would need actually in order to build this application. First of all we are importing the vendor autoload.php file which is located in the library we have downloaded. The path may be different for you as you have renamed to your directory. In order to follow with me just rename the directory to phpclient. After that we will be importing the json file which will be holding the information for you such as client id,secret,redirect_uri also rename this file to whatever you want and include it in the script file. After that we have started the session and made a object of Google_Client class which is a class of the library. And after we have invoked some methods on that such as setting the application name and setting the client.json file and adding some scopes i.e. what information we want from the api and lastly we have invoked the method of redirect_uri in which we have setted the redirect_uri

READ  How to Check Username Availability in PHP AJAX MySQL

 

After that make a new file name index.php which we will be executed whenever we launch the application In this file our sole motive will be to include the config file which we have written earlier and one last step is to redirect user to the different accounts page where user can select accounts

 

<?php

require_once('config.php');

$authUrl = $client->createAuthUrl();

header('location:'.$authUrl);

?>

 

After you execute this script you will be instantly redirected to the accounts page where you want to select your Google Account as shown below

 

 

 

After that we need to make a new file redirect.php where we will write the code to handle the authorization code and exchange it for the access token.Copy paste the below code

 

<?php

require_once("config.php");

if(isset($_SESSION['accessToken']))
{
    $client->setAccessToken($_SESSION['accessToken']);
}
else if(isset($_GET['code']))
{
    $access_token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
    $_SESSION['accessToken'] = $access_token;
}
else{
    header('location : index.php');
}

$oauth = new Google_Service_Oauth2($client);

$user = $oauth->userinfo->get();

$picture_link = $user->picture;

echo '<img width="200" height="200" src='.$picture_link;

echo '<br>';

echo "<h2>".$user->name."</h2>";


echo "<a href='logout.php'>Logout</a>";

?>

 

READ  How to Upload Files with PHP 7

In this script we are handling the authorization code which is coming in the url and exchanging it for the access token and setting the access token to the session variable if it is not set. For this we are first of all checking if the session variable is not set. If not set we set the session variable for the access token to the value of access token. The value of the access token is retreived by the library method of fetchAccessTokenWithAuthCode which is invoked on the client object. Last if condition is for checking if the authorization code is missing then we will redirect the user to the index.php file.

 

In the later half of the script we are fetching the photo and the name of the person and displaying it in the browser.

 

In order to successfully logout the user just copy paste the following code into a new file called logout.php

 

<?php

session_start();

unset($_SESSION);

session_destroy();

echo "<a href='index.php'>Click here to go to index.php</a>";


?>

Congratulations we are done making a complete application in which we have implemented Google Auth in PHP with the help of Client Library from scratch. Thanks for reading this post and if you like reading this and wants to read more of this please subscribe the blog below to get all the notications.

Leave a Reply

Close Menu