Create Login With Google Plus in Your Website With PHP

So you want to allow users to login into your website using their gmail credentials? You have seen various websites that allow their users to login in their websites using gmail, facebook, linked in, Microsoft, git hub credentials. It’s time to integrate it in your website. We will cover all the login system in our posts one by one and this one is dedicated to create Google Plus login for your website with PHP using OAuth2. Google offers many APIs like Google Maps, translate API, Analytics ApI etc. Today we will use its Google Plus API so lets proceed with our tutorial.

  infotuts-google-login

Live Demo        Download

Google offers API that uses OAuth2 for authorization and allows user to use their Google Plus credential to login in any website or application. First of all you need to get a Google client secret and key. Lets start our step step guide:

1> Login to Google API Console. Go to APIs and you will have to turn on Google Plus API.

google_plus_login

2> Go to APIs and Auth and then under credentials tab. Click on create new client ID as shown below.

Google_login_1

3> Now when you will have to enter your website path and the file path (redirect URI) to get your new client ID. You will need to enter these client ID and secret in your code later.

google_api_2-_infotuts

Now your client ID and client secret will be generated. If in above step you fill anything wrong then it can be edited and changed later.

Google_login_api-_3

4> Now you have to set Consent screen.

Google_login_4

5> In consent screen if you have entered Google Plus page path then you will have to approve connection. So Approve it by clicking on Plus page management and you are good to go.

google_login_5

Once all is done, we must get on and code our login system using API. First of all create two file api.php and login.php. In our api.php we will start user session and will ask user to login.

</p>
<?php
require_once 'lib/src/Google_Client.php';
require 'lib/src/contrib/Google_Oauth2Service.php';
require_once 'lib/src/contrib/Google_PlusService.php';
session_start();
 $api = new Google_Client();
 $api->setApplicationName("InfoTuts"); // Set Application name
 $api->setClientId('########################################'); // Set Client ID
 $api->setClientSecret('####################################'); //Set client Secret
 $api->setAccessType('online'); // Access method
 $api->setScopes(array('https://www.googleapis.com/auth/plus.login', 'https://www.googleapis.com/auth/plus.me', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile'));
 $api->setRedirectUri('http://www.infotuts.com/demo/googlelogin/login.php'); // Enter your file path (Redirect Uri) that you have set to get client ID in API console
 $service = new Google_PlusService($api);
 $URI = $api->createAuthUrl();
?>
<p style="text-align: justify;">

Now in our login.php file we need to authenticate user and will display his details if he enters his correct Google plus credentials. We have written a login() function to authenticate user and store his details in an array:

</p>

 function login(){
 session_start();
 $this->lib_include();
 $api = new Google_Client();
 $api->setApplicationName("InfoTuts");
 $api->setClientId('##########################################');
 $api->setClientSecret('#######################################');
 $api->setAccessType('online');
 $api->setScopes(array('https://www.googleapis.com/auth/plus.login', 'https://www.googleapis.com/auth/plus.me', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile'));
 $api->setRedirectUri('http://www.infotuts.com/demo/googlelogin/login.php');
 $service = new Google_PlusService($api);
 $oauth2 = new Google_Oauth2Service($api);
 $api->authenticate();
 $_SESSION['token'] = $api->getAccessToken();
 if (isset($_SESSION['token'])) {
 $set_asess_token = $api->setAccessToken($_SESSION['token']);
 }
 if ($api->getAccessToken()) {
 $data = $service->people->get('me');
 $user_data = $oauth2->userinfo->get(); ?>
<p style="text-align: justify;">

That’s all :) yes I have put the main code of both the files. You can download the working code and replace client secret, client ID and redirect URI and deploy it in your server.  You can destroy session on user log out. Its good to integrate this in your website as most of the users already have Google account so they do not need to create a new account. We can do a lot more with Googel API’s and I will try to cover as much as I can. Let me know in comments if you get stuck somewhere in implementing this in your web project.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  1. Hello,

    Very good tutorial, i want to use in my site, will you please explain how to redirect to login when try to access index.html (or .php) if they are not logged in.

    Reply
    thanks

    • Hello Chirag,

      Glad that you liked this tutorial. If user is not logged in and accessing any webpage that is supposed to be accessed after login you can check the session value if its not set then you can redirect him to the page you want.
      Here is a sample code to help you understand what I mean.

      if(!isset($_SESSION['token'])){
               header("Location: login.php");
          }
  2. Pingback: InfoTuts.com: Create Login With Google Plus in Your Website With PHP | facebooklikes

  3. Pingback: Login with Facebook in Your Website Using Facbook SDK for PHP InfoTuts

  4. sorry! the download is not working. i have subscribed through my email id and got a email too. but still error is showing up

  5. thanks
    Its really helpful for me, I am not PHP developer still I implement this facebook login very easily. Its very friendly tutorial
    Thanks once again, its give me a boost in my energy

    • Hi,

      Please make sure your all paths are correct and you user not using IP to access if you are in localserver. Please direct your ID to a domain name.
      use http://localhost. to access local server.

      Thanks:
      InfoTuts