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.

Post Tagged with ,

15 Responses so far.

  1. Chirag says:

    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

    • Sanjeev says:

      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. […] tutorial showing you how to make a “Log in with Google” button for your application and make it work with a little PHP magic on the […]

  3. […] friends we have already learnt how easily you can implement login with Google plus in your website using Open Auth. Today we will learn another easy way to implement Facebook login […]

  4. phani says:

    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

  6. Ashish vasani says:

    i am geetting an 500 internal server error when i a trying to run the code given above.

    • InfoTuts says:

      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

  7. runa says:

    I have subscribed through mentioned mail-id but download link is not coming. Not able to download.

  8. Aitor says:

    I have waited 24 hours and still can’t download the package. Could you help me please? Seems so interesting.

  9. Charles says:

    Hello Sanjeev, I suscribed to download the script but one day later, it still saying I need to subscribe to download it… Any chance you could send me the files to my email ? Thank you so much !!!

  10. kedar pawgi says:

    Hi i gone through you code its really simple and good. I would like to know , where i should include the above code or file so that those will be executed. Do i have to link those to google+ image, which of the above file need to be connected with the image of google+, Please explain need very urgently for my web site….


About Satyendra Yadav

I am a PHP and WordPress developer. I love to code and build something useful.