我的OpenId代码没有重定向到Google登录页面

时间:2011-07-31 17:55:19

标签: php openid

我有一些代码应该重定向到OpenId的Google登录页面

<?php
include '../connect.php';
require_once "common.php";

$path = $_GET['path'];


    if (empty( $path ) )
    {
        $error = "Expected an OpenID URL.";
        //include 'index.php';
        //exit(0);
    }
    else
    {
        echo "<p>Success, path: ".$path."</p>";
    }

    $consumer = getConsumer();

    // Begin the OpenID authentication process.
    $auth_request = $consumer->begin($path);   

    // No auth request means we can't begin OpenID.
    if (!$auth_request) 
    {
        echo "<p>Authentication error; not a valid OpenID.</p>";
    }

    $sreg_request = Auth_OpenID_SRegRequest::build(
                                     // Required
                                     array('nickname'),
                                     // Optional
                                     array('fullname', 'email'));

    if ($sreg_request) 
    {
        $auth_request->addExtension($sreg_request);
    }

    $policy_uris = $_GET['policies'];    

    $pape_request = new Auth_OpenID_PAPE_Request($policy_uris);
    if ($pape_request) 
    {
        $auth_request->addExtension($pape_request);
    }

    // Redirect the user to the OpenID server for authentication.
    // Store the token for this authentication so we can verify the response.

    // For OpenID 1, send a redirect.  For OpenID 2, use a Javascript
    // form to send a POST request to the server.
    if ($auth_request->shouldSendRedirect()) 
    {
        $redirect_url = $auth_request->redirectURL(getTrustRoot(), getReturnTo());     

        // If the redirect URL can't be built, display an error
        // message.
        if (Auth_OpenID::isFailure($redirect_url)) 
        {
            displayError("Could not redirect to server: " . $redirect_url->message);
        } 
        else 
        {
            // Send redirect.

            echo '<p>Would have redirected url</p>';
            header("Location: ".$redirect_url);
        }
    }        
    else 
    {
        // Generate form markup and render it.
        $form_id = 'openid_message';
        $form_html = $auth_request->htmlMarkup(getTrustRoot(), getReturnTo(),
                                               false, array('id' => $form_id));

        // Display an error if the form markup couldn't be generated;
        // otherwise, render the HTML.
        if (Auth_OpenID::isFailure($form_html)) 
        {
            echo "<p>Could not redirect to server: " . $form_html->message."</p>";
        } 
        else 
        {
            print $form_html;
        }                                   
    } 

    run ();
?>

然后页面应该返回到一个名为finish_auth.php的脚本(此处有很多调试语句):

<?php
require_once "common.php";
session_start();

function escape($thing) 
{
    return htmlentities($thing);
}

function run() 
{
    $consumer = getConsumer();
    var_dump($consumer); 
    echo '<p>test 2</p>';   

    // Complete the authentication process using the server's
    // response.
    $return_to = getReturnTo();

        var_dump($return_to); 
    echo '<p>test 3</p>';    
    $response = $consumer->complete($return_to);

        var_dump($response); 
    echo '<p>test 4</p>';     

    // Check the response status.
    if ($response->status == Auth_OpenID_CANCEL) 
    {
        // This means the authentication was cancelled.
        $msg = 'Verification canceled.';
         echo '<p>Canceled</p>';  
    } 
    else 
    if ($response->status == Auth_OpenID_FAILURE) 
    {
        echo '<p>Open Id Failure</p>';  
        // Authentication failed; display the error message.
        $msg = "OpenID authentication failed: " . $response->message;
    } 
    else 
    if ($response->status == Auth_OpenID_SUCCESS) 
    {
            echo '<p>SUCCESS</p>';  
        // This means the authentication succeeded; extract the
        // identity URL and Simple Registration data (if it was
        // returned).
        $openid = $response->getDisplayIdentifier();

        var_dump($openid); 

        $esc_identity = escape($openid);



        $success = sprintf('You have successfully verified ' .
                           '<a href="%s">%s</a> as your identity.',
                           $esc_identity, $esc_identity);

        if ($response->endpoint->canonicalID) 
        {
            $escaped_canonicalID = escape($response->endpoint->canonicalID);
            $success .= '  (XRI CanonicalID: '.$escaped_canonicalID.') ';
        }

        $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);

        $sreg = $sreg_resp->contents();

        var_dump($sreg); 
    echo '<p>test sreg</p>';         

        if (@$sreg['email']) 
        {
            $success .= "  You also returned '".escape($sreg['email']).
                "' as your email.";

                echo $success;
        }
        else
        {
            echo '<p>Not email success</p>';
        }


        if (@$sreg['nickname']) 
        {
            $success .= "  Your nickname is '".escape($sreg['nickname']).
                "'.";

            echo $success;
        }
        else
        {
                    echo '<p>Not nickname success</p>';
        }

        if (@$sreg['fullname']) 
        {
            $success .= "  Your fullname is '".escape($sreg['fullname']).
                "'.";

                                echo $success;
        }
        else
        {
                        echo '<p>Not full name success</p>';
        }

        $pape_resp = Auth_OpenID_PAPE_Response::fromSuccessResponse($response);

        if ($pape_resp) 
        {
            if ($pape_resp->auth_policies) 
            {
                $success .= "<p>The following PAPE policies affected the authentication:</p><ul>";

                foreach ($pape_resp->auth_policies as $uri) 
                {
                    $escaped_uri = escape($uri);
                    $success .= "<li><tt>$escaped_uri</tt></li>";
                }

                $success .= "</ul>";
            } 
            else 
            {
                $success .= "<p>No PAPE policies affected the authentication.</p>";
            }

            if ($pape_resp->auth_age) 
            {
                $age = escape($pape_resp->auth_age);
                $success .= "<p>The authentication age returned by the " .
                    "server is: <tt>".$age."</tt></p>";
            }

            if ($pape_resp->nist_auth_level) 
            {
                $auth_level = escape($pape_resp->nist_auth_level);
                $success .= "<p>The NIST auth level returned by the " .
                    "server is: <tt>".$auth_level."</tt></p>";
            }
    } 
    else 
    {
            $success .= "<p>No PAPE response was sent by the provider.</p>";
    }
}

    //include '../index.php';
                           //  header( 'Location: http://www.comehike.com' );


                             // $_SESSION['user_id'] = $user_id;
                             // $_SESSION['user_email']  = $row['user_email'];
                             // $_SESSION['user_lat']  = $row['lat'];
                             // $_SESSION['user_lng']  = $row['lng'];
                             // $_SESSION['first_name'] = $row['first_name'];
}

run();


?>

您可以通过此处测试当前的用户体验:

http://www.comehike.com/account/member_home.php

点击“使用您的Google帐户登录”链接。

我想知道的是 - 如何让页面实际重定向到谷歌页面进行登录?当前的ux不会带您到那里验证您可以使用您的Gmail帐户登录到comehike.com

谢谢!

0 个答案:

没有答案