Question

I have this modified google-example connection code for lightOpenId:

<?php
require_once('../db.php');
session_start();
# Logging in with Google accounts requires setting special identity, so this example shows how to do it.
require 'openid.php';
try {
    # Change 'localhost' to your domain name.
    $openid = new LightOpenID('127.0.0.1');
    if(!$openid->mode) {
        if(isset($_GET['login'])) {
            $openid->identity = 'https://www.google.com/accounts/o8/id';
            $openid->required = array('contact/email');
            $openid->optional = array('namePerson', 'namePerson/friendly');
            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 {
        if ($openid->validate()) {
            $_SESSION['auth'] = $openid->identity;

            $userinfo = $openid->getAttributes();
            print_r($openid->required);
            echo $_SESSION['auth'];

            $stmt = $dbh->prepare("INSERT INTO users(id,name,email) VALUES(:id,:name,:email)");
            $stmt->execute(array(":id" => $openid->identity,
                                ":name" => "name",
                                ":email" => "name"));
        }
    }
} catch(ErrorException $e) {
    echo $e->getMessage();
}

My problem is that $openid->required does not return anything. The array itself does not even contain an empty "contact/email" field. What am I doing wrong here?

Was it helpful?

Solution

$openid->required isn't preserved between requests, but you don't have to use it.

As for $userinfo being empty, it only contains fields returned by the provider (for example, no fields), regardless of what you requested.

The provider may return the fields you have requested, but it doesn't have to, and doesn't even have to support returning any additional data (as this is a protocol extension).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top