Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use open id as login system

Tags:

php

openid

I want to try to use open id as login system on the site that I am going to make. I've check out this site, and I'm not pretty sure how this all works and how to make it work. http://remysharp.com/2007/12/21/how-to-integrate-openid-as-your-login-system/

What is clear right now is that open id uses urls instead of passwords. Can you give me some links to help me get started with this, Do I need to know advanced php to make this work for me. I only know the basics of php.

like image 407
Wern Ancheta Avatar asked Dec 16 '10 10:12

Wern Ancheta


1 Answers

Thanks to some other comment on Stackoverflow.com I came to learn about LightOpenId. It is really easy to use.

The example code just works(without any configuration):

<?php
require 'openid.php';
try {
    $openid = new LightOpenID;
    if(!$openid->mode) {
        if(isset($_POST['openid_identifier'])) {
            $openid->identity = $_POST['openid_identifier'];
            header('Location: ' . $openid->authUrl());
        }
?>
<form action="" method="post">
    OpenID: <input type="text" name="openid_identifier" /> <button>Submit</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();
}

Using google as openid provider.

<?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();
}
like image 162
Alfred Avatar answered Sep 19 '22 01:09

Alfred