2

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

diagonalbatman
  • 17,340
  • 3
  • 31
  • 31
siddesh savant
  • 89
  • 1
  • 6
  • 12
  • its called openid look on this link http://technofriends.in/2008/11/10/what-is-openid-and-how-to-use-your-gmail-account-as-openid/ – Haim Evgi Feb 02 '11 at 12:00

3 Answers3

5

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.
Alfred
  • 60,935
  • 33
  • 147
  • 186
  • Thanks a lot..Can u give full simple example... – siddesh savant Feb 02 '11 at 12:09
  • this is a full simple example ;). When you download tarball you can just run this example => http://gitorious.org/lightopenid/lightopenid/archive-tarball/master – Alfred Feb 02 '11 at 12:17
  • ya i got it...But how about the logout..... – siddesh savant Feb 02 '11 at 12:28
  • to login you store `$openid->identity` in `$_session`. You should [regenerate session])(http://www.php.net/manual/en/function.session-regenerate-id.php) after you store data in the `$_SESSION` to prevent session fixation. To logout you just destroy session using [session_destroy](http://php.net/manual/en/function.session-destroy.php). 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. – Alfred Feb 02 '11 at 12:37
0

it's authentication through OpenID. Read this http://code.google.com/apis/accounts/docs/OpenID.html

MeelStorm
  • 624
  • 6
  • 11
0

see

http://code.google.com/apis/accounts/docs/OpenID.html

enthusiastic
  • 732
  • 2
  • 9
  • 18