Here are the simple steps to setup google sign-in for websites. We know, OpenID is a elegant, safe and a easy way to login into a website without filling up the registration form. It’s need to have the account to an openID provider, one google account and it will login into the site with the account. Here is the way to implement this with the google account while the source code is the same for all providers.

sign in with google
                                         Image Credit: Github.com

Here Is The Full Task Instruction About Google Sign-in for Websites Setup:

You don’t have to deal with the respective user authentication.  You just need to provide a link to the google pages in which the users are to

1. give credentials  – while no need if already logged in the gmail for instance-

Google Sign-in for Websites
                           Image Credit: Google.com

2. give authorization to the google account to give you a bit personal data as you request.

Google Sign-in for Websites
                     Image Credit: Google.com

Then, the users are to redirect to the page on your website you chose and thus, you are able to retrieve his personal information.

Some codes to write

There are two parts in the codes:

  1. The first part is about the code to retrieve users personal data. You are to execute the code once when the user click on the “Login with my Google account”.
  2. The second part is about the code to deliver the login button. You may display this login button on several web pages, sidebar or wherever you want  on your website such that the code run each time as you deliver the button.

We are to use a one file PHP – OpenID library, download from LightOpenID. You need to put this file in to your project folder.

Here is the login.php file, the pages where users are redirected after given the authorization and credentials to Google. This code retrieves and display the users personal data.

 

<?php // login.php
require_once 'openid.php';
$openid = new LightOpenID("example.com");

if ($openid->mode) {
    if ($openid->mode == 'cancel') {
        echo "User has canceled authentication!";
    } elseif($openid->validate()) {
        $data = $openid->getAttributes();
        $email = $data['contact/email'];
        $first = $data['namePerson/first'];
        echo "Identity: $openid->identity <br>";
        echo "Email: $email <br>";
        echo "First name: $first";
    } else {
        echo "The user has not logged in";
    }
} else {
    echo "Go to index page to log in.";
}
?>

Now, replace Example.com with the domain name of your’s. If you already have a running login/signup system on your website, you may want to have a openid_identity field with your table users in the database and stored in it $openid->identity. It is an unique ID which Google give you to identify the users. The other fields can be changed if the user are to change his Google profile.

at the last step, the code to deliver the button, let’s say on home page, index.php. It will create/call a Google window and tell the google window to get redirect to your website site after.

 

<?php // index.php
require_once 'openid.php';
$openid = new LightOpenID("example.com");

$openid->identity = 'https://www.google.com/accounts/o8/id';
$openid->required = array(
  'namePerson/first',
  'namePerson/last',
  'contact/email',
);
$openid->returnUrl = 'http://my-domain.com/login.php'
?>

<a href="<?php echo $openid->authUrl() ?>">Login with Google</a>

 

Here you are to change the returnUrl field:-  put the accurate URL of that PHP pages. And once gain, be sure to replace Example.com with your that domain name.

Persisting the authenticated state

To persue the state into your application, you may use the sessions. If you do have the user’s data, you may authenticate the user into your application.

session_start();
$_SESSION['user'] = $email;

To have a test whether or not the user logged in into your application-

session_start();
if (isset($_SESSION['user'])) {
    // user logged in
} else {
    // user not logged in
}

To get logout the user, just destroy the session:

session_start();
session_destroy();

 

Thus you can setup the google sign-in for websites easily.

mm
Hay, It's Mehedi Hasan. Casual Guy From Asia. I Like To Write And Share My Thoughts On Different Aspects Of My Knowledge Like Credit Cards,Education,Travel Guide,Biography,Technology,Mining Technology And Others Subject. Hope You Guys Are Like My Writing. Thanks