Join TalkersCode Now !

Get Latest Tutorials And Links On Web Development
And Get Existing Offers Via Email

Login With Google Using PHP

Tuesday, Mar 7, 2017

Tags:- PHP


In this tutorial we will show you how to login with your Google account using php it is also known as Open Authentication Login System (oAUTH) by using this method you can login to your google account right from your website.You may also like login with facebook using PHP.


Login With Google Using PHP


To Login With Google Using PHP It Takes Only Two Steps:-

  1. Create a new project on google and collect all details
  2. Make a PHP file and define markup and scripting


Step 1.Create a new project on google and collect all details

You have to create a new project on google using this link https://code.google.com/apis/console/. Then get all the required details like client ID, client secret and API keys then configure application details and enter you own details like client ID, client secret and redirect url.You can find this file in src folder.You may also like login with twitter using PHP.



Step 2.Make a PHP file and define markup and scripting

We make a PHP file and save it with a name login.php

require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_Oauth2Service.php';
session_start();

$client = new Google_Client();
$client->setApplicationName("Sample Google Login Application");

$oauth2 = new Google_Oauth2Service($client);

if (isset($_GET['code'])) 
{
 $client->authenticate($_GET['code']);
 $_SESSION['token'] = $client->getAccessToken();
 $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
 header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
 return;
}

if (isset($_SESSION['token'])) 
{
 $client->setAccessToken($_SESSION['token']);
}

if (isset($_REQUEST['logout'])) 
{
 unset($_SESSION['token']);
 $client->revokeToken();
}

if ($client->getAccessToken()) 
{
 $user = $oauth2->userinfo->get();

 $email = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
 $img = filter_var($user['picture'], FILTER_VALIDATE_URL);
 $personMarkup = "$email<div><img src='$img?sz=100'></div>";

 $_SESSION['token'] = $client->getAccessToken();
}
else 
{
 $authUrl = $client->createAuthUrl();
}

if(isset($personMarkup)): print $personMarkup; //This will print user Information 
endif;

if(isset($authUrl)) 
{
 print "<a class='login' href='$authUrl'>Login With Google</a>";
}
else 
{
 print "<a class='logout' href='?logout'>Google Logout</a>";
}

Thats all, this is how to login with Google using PHP.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email

Are You Starting A New Blog
Do You Want Best Web Hosting?

Try World's Best Web Hosting Provider BlueHost
At Just $3.95/mo

Hurry Up! Limited Time Offer

Yes, I Want To Check Out BlueHost

Check The World's Best SEO And All-In-One Marketing Toolkit
For Digital Marketing Professionals SEMRUSH

Check Out SEMRUSH