【问题标题】:How to implement openid google, yahoo and othersgoogle、yahoo等openid如何实现
【发布时间】:2011-05-18 10:06:18
【问题描述】:

我正在尝试实现 OpenID,为此我下载了 http://www.openidenabled.com/php-openid 并从中获取了 Auth 文件夹,并将任何内容更改为 localhost 目录并创建了一个 index.php 文件,其代码如下:

<?php
    if (!isset($_POST['submit'])) {
?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
        <title>OPENID TESTING</title>
        </head>
        <body>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            Sign in with your OpenID: <br/>
            <input type="text" name="id" size="30" />
            <br />
            <input type="submit" name="submit" value="Log In" />
        </form>
        </body>
        </html>
<?php
    } else {

        // check for form input
        if (trim($_POST['id'] == '')) {
            die("ERROR: Please enter a valid OpenID.");
        }

        // include files
        require_once "Auth/OpenID/Consumer.php";
        require_once "Auth/OpenID/FileStore.php";

        // start session (needed for YADIS)
        session_start();

        // create file storage area for OpenID data
        $store = new Auth_OpenID_FileStore('./oid_store');

        // create OpenID consumer
        $consumer = new Auth_OpenID_Consumer($store);

        // begin sign-in process
        // create an authentication request to the OpenID provider

        $auth = $consumer->begin($_POST['id']);
        if (!$auth) {
            die("ERROR: Please enter a valid OpenID.");
        }

        // redirect to OpenID provider for authentication
        $url = $auth->redirectURL('http://localhost/', 'http://localhost/oid_return.php');
        header('Location: ' . $url);
    }
?>

现在,当我尝试从 worldpress 填写 id 或 myopenid.com 被识别并将我转移到提供商页面进行身份验证时,但这不会发生在 Google、yahoo 或其他服务提供商案例中。我必须为 Google 或 Yahoo 实施什么

【问题讨论】:

    标签: openid google-openid openid-provider openid-selector


    【解决方案1】:

    我正在使用 PHP LightOpenID 库 (see on gitorious) 使用户能够使用他们的 Google 帐户登录我的网站。对于其他 OpenID 提供者,您只需更改 $openid-&gt;identity 字段。它为我们处理所有的身份验证流程。您无需为令牌和其他东西烦恼。

    这里是我显示“使用 Google 登录”链接的页面:

    <?php
    require_once 'openid.php';
    $openid = new LightOpenID;
    
    $openid->identity = 'https://www.google.com/accounts/o8/id';
    $openid->required = array('contact/email');
    $openid->returnUrl = 'http://my-website.com/landing-login.php'
    ?>
    
    <a href="<?php echo $openid->authUrl() ?>">Login with Google</a>
    

    当点击链接时,会出现一个谷歌页面,要求他进行身份验证和/或授权您检索他的电子邮件。

    然后他将被重定向到登陆页面$openid-&gt;returnUrl。该页面的代码应该是:

    <?php
    require_once 'openid.php';
    $openid = new LightOpenID;
    
    if ($openid->mode) {
        if ($openid->mode == 'cancel') {
            // User has canceled authentication
        } elseif($openid->validate()) {
            // Yeah !
            $data = $openid->getAttributes();
            $email = $data['contact/email'];
        } else {
            // The user has not logged in via Google
        }
    } else {
        // The user does not come from the link of the first page
    }
    ?>
    

    如果你想从用户那里获取更多信息,你必须在第一页将他们添加到$openid-&gt;required。例如:

    $openid->required = array(
      'contact/email',
      'namePerson/first',
      'namePerson/last'
    );
    

    如果用户接受,您可以在第二页中获取他的名字和姓氏:

    $name = $data['namePerson/first'] . " " . $data['namePerson/last'];
    

    希望有帮助!

    【讨论】:

    • 我尝试添加 'namePerson/first'、'namePerson/last' 似乎不适用于 google 或 Yahoo(我确实收到了电子邮件和其他值)。知道为什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    相关资源
    最近更新 更多