Login with Google account is useful feature for any community website and other type of website. Today we implement login with Google account step by step.
Here we first get know the complete steps then we elaborate the process of implementation.
Go below to download the complete source code of Login with Google Account using PHP
Complete steps to implement Login with Google Account using PHP
- Create Google API credentials.
- Download Google API Client Library for PHP.
- Create Google Config PHP file
- Create an index PHP file
- Create logout
Steps to Create Google API credentials
- Go to https://console.developers.google.com/ Google developer console by the given link.
2. Click on create project like shown on image below,
3. After that, if you have already multiple projects then select one which you create at a time.
4. Click on the OAuth option which is available on the sidebar of the Google developer console.
5. Now we create OAuth Client ID and secret ID by filling the OAuth form like below and hit the Save button which is available on the very bottom of the page.
6. After the above steps, we got the Client ID and Clent secret by creating credentials steps,
Click on CREATE CREDENTIALS
7. Select Web Application from the dropdown
8. After that fille the form put the application name and redirection UEL (After successful logout where the page goes to).
9. Now you get your client ID and secret ID
Google API Client Library for PHP
Above are the complete steps to creating Google OAuth client ID and secret ID. Now we set up Google API Client Library for PHP by using composer and CLI or Git Bash.
If you do not know how to install composer on the system check here:
Install Composer on Linux and Windows in 3 Steps
Run the below command on your CLI or Git Bash to set up or download the Google API client PHP library,
composer require google/apiclient:"^2.0"
After that we setup the config file where we set the below things,
- Set the OAuth 2.0 Client ID
- Set the OAuth 2.0 Client Secret key
- Set the OAuth 2.0 Redirect URI
config.php File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php //Include Google Client Library for PHP autoload file require_once 'vendor/autoload.php'; //Make object of Google API Client for call Google API $google_client = new Google_Client(); //Set the OAuth 2.0 Client ID $google_client->setClientId('Set the OAuth 2.0 Client ID'); //Set the OAuth 2.0 Client Secret key $google_client->setClientSecret('Set the OAuth 2.0 Client Secret key'); //Set the OAuth 2.0 Redirect URI $google_client->setRedirectUri('http://localhost/loginWithGoogle/index.php'); $google_client->addScope('email'); $google_client->addScope('profile'); //start session on web page session_start(); ?> |
On above we also setup session and mentioned which fields are we get after Google login.
Now we create our index file where we put our design and get the user data and store it to session and print on the web page. Also, set up the button for login with Google.
index.php File
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 84 85 86 87 88 89 90 91 92 93 94 |
<?php //Include Configuration File include('config.php'); $login_button = ''; //This $_GET["code"] variable value received after user has login into their Google Account redirct to PHP script then this variable value has been received if(isset($_GET["code"])) { //It will Attempt to exchange a code for an valid authentication token. $token = $google_client->fetchAccessTokenWithAuthCode($_GET["code"]); //This condition will check there is any error occur during geting authentication token. If there is no any error occur then it will execute if block of code/ if(!isset($token['error'])) { //Set the access token used for requests $google_client->setAccessToken($token['access_token']); //Store "access_token" value in $_SESSION variable for future use. $_SESSION['access_token'] = $token['access_token']; //Create Object of Google Service OAuth 2 class $google_service = new Google_Service_Oauth2($google_client); //Get user profile data from google $data = $google_service->userinfo->get(); //Below you can find Get profile data and store into $_SESSION variable 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']; } } } //This is for check user has login into system by using Google account, if User not login into system then it will execute if block of code and make code for display Login link for Login using Google account. if(!isset($_SESSION['access_token'])) { //Create a URL to obtain user authorization $login_button = '<a href="'.$google_client->createAuthUrl().'"><img src="https://androidexample365.com/content/images/2019/11/GoogleSignUpDark.png" /></a>'; } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PHP Login using Google Account</title> <meta content='width=device-width, initial-scale=1, maximum-scale=1' name='viewport'/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" /> </head> <body> <div class="container"> <br /> <h2 align="center">PHP Login using Google Account</h2> <br /> <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 align="center">'.$login_button . '</div>'; } ?> </div> </div> </body> </html> |
All the line by line explainations are commented on the source code.
Now we create logout file where we revoke the session data.
logout.php File
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php include('config.php'); $accesstoken=$_SESSION['access_token']; //Reset OAuth access token $google_client->revokeToken($accesstoken); //Destroy entire session data. session_destroy(); //redirect page to index.php header('location:index.php'); ?> |
Here is the complete source code of Login with Google Account using PHP.
Download Complete source code of Login with Google Account using PHP,
[sociallocker][wpdm_package id=’2546′] [/sociallocker]
Also Check:
- Send Mail Attachments with PHP Mail()
- Selection in CSS | CSS ::selection
- How to Integrate Google Translate on website
Happy Coding..!
[…] Login with Google Account using PHP Step by Step […]
[…] Login with Google Account using PHP Step by Step […]
[…] Login with Google Account using PHP Step by Step […]
[…] Login with Google Account using PHP Step by Step […]
[…] Login with Google Account using PHP Step by Step […]
[…] Login with Google Account using PHP Step by Step […]
[…] Login with Google Account using PHP Step by Step […]
[…] Login with Google Account using PHP Step by Step […]
[…] Go to https://console.developers.google.com/ then click on “New Project”. You can also check this link to get complete action to get credentials. Here https://codingtasks.net/login-with-google-account-using-php-step-by-step/#Steps_to_Create_Google_API_c… […]
Working Perfectly with localhost but not working on live domain. Actually I’m using this with many domains but in 1 of my domain it is not working with no error. please help
Please give some lines of code
[…] Login with Google Account using PHP Step by Step … […]
[…] Login with Google Account using PHP Step by Step … […]
[…] Login with Google Account using PHP Step by Step … […]
[…] Login with Google Account using PHP Step by Step … […]
[…] Login with Google Account using PHP Step by Step … […]
[…] Login with Google Account using PHP Step by Step […]
[…] Login with Google Account using PHP Step … – PHPCODER.TECH […]
[…] Login with Google Account using PHP Step by Step … […]