Login to site using Gmail

13,736

This is very easy to accomplish with LightOpenID. They have an example available with that use case in mind.

<?php
# Logging in with Google accounts requires setting special identity, so this example shows how to do it.
require 'openid.php';
try {
    $openid = new LightOpenID;
    if(!$openid->mode) {
        if(isset($_GET['login'])) {
            $openid->identity = 'https://www.google.com/accounts/o8/id';
            header('Location: ' . $openid->authUrl());
        }
?>
<form action="?login" method="post">
    <button>Login with Google</button>
</form>
<?php
    } elseif($openid->mode == 'cancel') {
        echo 'User has canceled authentication!';
    } else {
        echo 'User ' . ($openid->validate() ? $openid->identity . ' has ' : 'has not ') . 'logged in.';
    }
} catch(ErrorException $e) {
    echo $e->getMessage();
}

To get you up and running is really simple(I assume *nix like system):

  • I have www folder mapped to my server.
  • I assume you have server it running on localhost.
  • I assume you have google-chrome installed.

alfred@alfred-laptop:~/www$ wget http://gitorious.org/lightopenid/lightopenid/archive-tarball/master
--2011-02-02 13:21:30--  http://gitorious.org/lightopenid/lightopenid/archive-tarball/master
Resolving gitorious.org... 87.238.52.168
Connecting to gitorious.org|87.238.52.168|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 17104 (17K) [application/x-gzip]
Saving to: `master'

100%[======================================>] 17,104      --.-K/s   in 0.04s   

2011-02-02 13:21:30 (386 KB/s) - `master' saved [17104/17104]

alfred@alfred-laptop:~/www$ tar xfz master 
alfred@alfred-laptop:~/www$ google-chrome http://localhost/lightopenid-lightopenid/example-google.php
Created new window in existing browser session.
Share:
13,736
siddesh savant
Author by

siddesh savant

I work for a web development company.Bit new in this field.Eager to learn new things.

Updated on June 28, 2022

Comments

  • siddesh savant
    siddesh savant almost 2 years

    I am implemint a login in my site.I want to implement the login somewhat how it is done in stackoverflow.But with only one option of gmail. 1)The user must have chose login type as gmail 2)He must enter his gmail username and password 3)He must be redirecred back to site

    i am very new in this ..Any help plz

  • siddesh savant
    siddesh savant about 13 years
    Thanks a lot..Can u give full simple example...
  • Alfred
    Alfred about 13 years
    this is a full simple example ;). When you download tarball you can just run this example => gitorious.org/lightopenid/lightopenid/archive-tarball/master
  • siddesh savant
    siddesh savant about 13 years
    ya i got it...But how about the logout.....
  • Alfred
    Alfred about 13 years
    to login you store $openid->identity in $_session. You should [regenerate session])(php.net/manual/en/function.session-regenerate-id.p‌​hp) after you store data in the $_SESSION to prevent session fixation. To logout you just destroy session using session_destroy. Keep in mind that users still needs to logout of Google. If not when he visit your site again and clicks the login link he just gets logged. You can't off course logout the user out of Google. That's something the user has to do himself.