Login with Google Account using PHP
PHP Google OAuth API allows users to login in a web application with Google credentials. In this tutorial, you will learn how to build a Login with Google Account using PHP SDK.This is a very simple example, you can just copy-paste, and change according to your requirement.
Before starting to implement the Login with Google Account using PHP, look files structure:
- login-with-google-account-using-php
- vendor
- config.php
- index.php
- profile.php
- logout.php
Step 1: Login to Google Developers Console
Go to the Google API Console and Log In with your Google account.
Step 2: Select a project link
Click on the Select a project link.
Step 3: Create a New Project
Click on the New Project link and you enter the Project Name and other details.
Step 4: Create Credentials
Click on the Credentials from the left sidebar, and go to the Create Credentials screen tab and select OAuth client ID.
Step 5: OAuth for Web Application
Next, Choose the web application > type the name > enter the redirect URL > Click on the create button
Step 6: Client credentials
Click Client credentials > OAuth client ID to create a new set of credentials for our application.
Step 6:The Client id and Client secret
Finally, you have got the client id and client secret, copy them and save
Install the Google PHP SDK Client Library
Install it using
Composer
or install the library files manually. Click and Download
1 2 |
//Use Composer $composer require google/apiclient:"^2.0" |
Step 7: Client Library Integration
Create file named
config.php
In this file, we will put the google secret key, secret id and redirect URL.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php // Start session if(!session_id()){ session_start(); } require_once 'vendor/autoload.php'; $profileInfo = array(); // Google API configuration : Google client ID and Google client Secret define('YOUR_CLIENT_ID', 'YOUR-CLIENT-ID'); define('YOUR_CLIENT_SECRET', 'YOUR-CLIENT-SECRET'); define('REDIRECT_URI', 'https://domain.com/login-with-google-account-using-php/index.php'); // create Client Request to access Google API $client = new Google_Client(); $client->setApplicationName('Login to TechArise.com'); $client->setClientId(YOUR_CLIENT_ID); $client->setClientSecret(YOUR_CLIENT_SECRET); $client->setRedirectUri(REDIRECT_URI); $client->addScope("email"); $client->addScope("profile"); ?> |
Step 8: Create file index.php
Create an
index.php
file. In this PHP file we will show the google login.
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 |
<?php // Include configuration file require_once 'config.php'; // authenticate code from Google OAuth Flow if (isset($_GET['code'])) { $profileInfo = array(); $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); $_SESSION['token'] = $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; // get google account info $oauth_uid = $google_account_info->id; $email = $google_account_info->email; $first_name = $google_account_info->givenName; $last_name = $google_account_info->familyName; $gender = $google_account_info->gender; $picture = $google_account_info->picture; $locale = $google_account_info->locale; $profileInfo = array("oauth_uid"=>$oauth_uid, "first_name"=>$first_name , "last_name"=>$last_name , "email"=>$email , "gender"=>$gender , "picture"=>$picture, "locale"=>$locale); $_SESSION['profileInfo'] = $profileInfo; header("Location:profile.php"); // now you can use this profile info to create account in your website and make user logged in. } else { $createAuthUrl = $client->createAuthUrl(); ?> <!DOCTYPE html> <html> <head> <meta name="google-site-verification" content="jlByZLO42gqjZgfAEj29-VYfJC2hEwUj1KSeQpiGJcI" /> <link rel="canonical" href="https://www.techarise.com/" /> <meta name="author" content="TechArise"> <meta name="description" content="Learn Web Development Tutorials & Web Developer Resources, PHP, MySQL, jQuery, CSS, XHTML, jQuery UI, CSS3, HTML5, HTML, web design, webdesign, with TechArise tutorials. View live demo"> <meta name="keywords" content="TechArise, tutorial TechArise, tutorials, freebies, resources, web development, webdev, demo, PHP, MySQL, jQuery, CSS, XHTML, jQuery UI, CSS3, HTML5, HTML, web design, webdesign, php script, dynamic web content" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> Login with Google Account using PHP | Tech Arise</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> <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.5/js/bootstrap.min.js"></script> <link rel='stylesheet' id='font-awesome-5-css' href='https://techarise.com/wp-content/themes/v1/css/fontawesome-all.css?ver=5.7.2' type='text/css' media='all' /> <link rel="icon" type="image/ico" href="https://techarise.com/wp-content/themes/v1/favicon.ico"> <script data-ad-client="ca-pub-1169273815439326" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> </head> <body class=""> <div role="navigation" class="navbar navbar-default navbar-static-top" style="background: #273E4A;"> <div class="container"> <div class="navbar-header"> <button data-target=".navbar-collapse" data-toggle="collapse" class="navbar-toggle" type="button"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a href="https://techarise.com" class="navbar-brand" style="color:#FFF;">TECHARISE.COM</a> </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li class="active"><a href="https://techarise.com">Login with Google</a></li> </ul> </div> </div> </div> <h2><a href="#">Login with Google Account using PHP</a></h2> </div> <div style="padding: 15px 0px 15px 0px;"> <a class="btn btn-danger" role="button" title="Login with Google " href="<?php print $createAuthUrl;?>"><i class="fa fa-google" aria-hidden="true"></i> Login with Google</a> </div> </div> </body> </html> <?php } ?> |
Step 9: Create a profile.php
Create a
profile.php
file. In this file, we will show logged in user information like first_name, last_name email, picture and more.
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 |
<?php $profileInfo = array(); $output = ''; // Start session if(!session_id()){ session_start(); $profileInfo = $_SESSION['profileInfo']; } ?> <!DOCTYPE html> <html> <head> <meta name="google-site-verification" content="jlByZLO42gqjZgfAEj29-VYfJC2hEwUj1KSeQpiGJcI" /> <link rel="canonical" href="https://www.techarise.com/" /> <meta name="author" content="TechArise"> <meta name="description" content="Learn Web Development Tutorials & Web Developer Resources, PHP, MySQL, jQuery, CSS, XHTML, jQuery UI, CSS3, HTML5, HTML, web design, webdesign, with TechArise tutorials. View live demo"> <meta name="keywords" content="TechArise, tutorial TechArise, tutorials, freebies, resources, web development, webdev, demo, PHP, MySQL, jQuery, CSS, XHTML, jQuery UI, CSS3, HTML5, HTML, web design, webdesign, php script, dynamic web content" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> Login with Google Account using PHP | Tech Arise</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> <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.5/js/bootstrap.min.js"></script> <link rel='stylesheet' id='font-awesome-5-css' href='https://techarise.com/wp-content/themes/v1/css/fontawesome-all.css?ver=5.7.2' type='text/css' media='all' /> <link rel="icon" type="image/ico" href="https://techarise.com/wp-content/themes/v1/favicon.ico"> <script data-ad-client="ca-pub-1169273815439326" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> </head> <body class=""> <div role="navigation" class="navbar navbar-default navbar-static-top" style="background: #273E4A;"> <div class="container"> <div class="navbar-header"> <button data-target=".navbar-collapse" data-toggle="collapse" class="navbar-toggle" type="button"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a href="https://techarise.com" class="navbar-brand" style="color:#FFF;">TECHARISE.COM</a> </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li class="active"><a href="https://techarise.com">Login with Google</a></li> </ul> </div> </div> </div> <h2><a href="#">Login with Google Account using PHP</a></h2> </div> <div style="padding: 15px 0px 15px 0px;"> <?php if(!empty($profileInfo)){ $output .= '<img src="'.$profileInfo['picture'].'">'; $output .= '<p><b>Google ID:</b> '.$profileInfo['oauth_uid'].'</p>'; $output .= '<p><b>Name:</b> '.$profileInfo['first_name'].' '.$profileInfo['last_name'].'</p>'; $output .= '<p><b>Email:</b> '.$profileInfo['email'].'</p>'; $output .= '<p><b>Gender:</b> '.$profileInfo['gender'].'</p>'; $output .= '<p><b>Locale:</b> '.$profileInfo['locale'].'</p>'; $output .= '<p><a href="logout.php">Logout</a></p>'; }else{ $output = '<h3 style="color:red">Some problem occurred, please try again.</h3>'; } ; echo $output; ?> </div> </div> </body> </html> |
Step 9: Create a logout.php
The User click the logout button, user redirects on this file and the user session destroyed. And redirect to login.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php // Include configuration file require_once 'config.php'; // Remove token and user data unset($_SESSION['token']); unset($_SESSION['profileInfo']); // Reset OAuth access_token $client->revokeToken(); // Destroy all session data session_destroy(); // Redirect to index page header("Location:index.php"); ?> |