如何实现google、yahoo等的openid

发布于 2024-11-08 04:13:49 字数 2080 浏览 0 评论 0原文

我正在尝试实现 OpenID,为此我下载 http://www.openidenabled.com/php-openid< /a> 并从中选择 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 或 myopenid.com 填写 id 时,它会被识别并转移给我到提供商页面进行身份验证,但这没有发生与谷歌、雅虎或其他服务提供商的情况。我必须为 Google 或 Yahoo 实施什么

I am trying to implement OpenID and for that I download http://www.openidenabled.com/php-openid and from that I picked up Auth folder with changing any thing to localhost directory and created a index.php file and whose code is as below:

<?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);
    }
?>

Now when I try to fill id from worldpress or myopenid.com is recognised and transfer me to the provider page for authentication but this is not happening with Google, yahoo or others service providers case. What I have to implement for Google or Yahoo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初熏 2024-11-15 04:13:49

我正在使用 PHP LightOpenID 库(参见 gitorious)来使用户能够登录我的网站和他们的 Google 帐户。对于其他 OpenID 提供商,您只需更改 $openid->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>

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

然后他将被重定向到登陆页面$openid->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->required 中。例如:

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

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

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

希望有帮助!

I am using the PHP LightOpenID library (see on gitorious) to make users able to log into my website with their Google account. For other OpenID provider, you just have to change the $openid->identity field. It handles all the authentication flow for us. You don't need to bother about token and stuff.

Here the page where I display the "Login with Google" link :

<?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>

When the click on the link, a Google page will appear ask him to authenticate and/or authorize you to retrieve his email.

Then he will be redirect to the landing page $openid->returnUrl. The code for that page should be :

<?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
}
?>

If you want to retrieve more info from the user, you have to add them to $openid->required in the first page. For instance :

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

will let you, if the user accepts it, to get his first and last names as well in the second page :

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

Hope that helps !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文