页面重定向和存储OpenID用户信息

时间:2013-11-08 05:22:37

标签: php redirect openid

我目前正在实施lightopenid。它部分工作但我在保留凭据的同时重定向用户有困难。下面的示例让用户通过谷歌登录,然后据称重定向到用户访问页面,在那里显示他们的名字。 useraccess.php中没有显示任何内容。如何正确地从openID重定向用户的凭据

的login.php

<?php
# Logging in with Google accounts requires setting special identity, so this example shows how to do it.
session_start();
require 'openid.php';

try {

    $openid = new LightOpenID('http://mydomain.com');                           // akshat - declared an object of class lightopenid.. this is listed in openid.php
    if(!$openid->mode) {

        if(isset($_GET['login'])) {

            $openid->identity = 'https://www.google.com/accounts/o8/site-xrds?hd=mydomain.com'; //this can be changed as you know...      

            $openid->required = array('namePerson/friendly', 'contact/email' , 'contact/country/home', 'namePerson/first', 'pref/language', 'namePerson/last'); // akshat - line added by me from after reading from the net....

            header('Location: ' . $openid->authUrl());    
        }
?>
<script type="text/javascript">
//change to jquery so it is compatible with cross browsers
window.onload=function(){
document.getElementById("lform").submit();
}
</script>
<form name="form" id="lform" action="?login" method="post"> </form>
<?php
    } elseif($openid->mode == 'cancel') {
        echo 'User has canceled authentication for Your Domain !';
    } else {                                                // FETCH USER INFO HERE
                $fname = $openid->ret_fname();              // fetching user first name...
        $lname = $openid->ret_lname();                  // fetching user last name...
        $email = $openid->ret_email();                  // fetching user email...
        $lang = $openid->ret_lang();                    // fetching user language...
                session_start();

                // use it as required. I set them in session !
                $_SESSION['admin']['emailID'] = $email;          //put email id in session.
                $_SESSION['admin']['fname'] = $fname;            //put first name also in session.
                $_SESSION['admin']['lname'] = $lname;            //put last name also in session.

                header("Location: www.example.com/useraccess.php");              // Go back to the calling application !

    }
} catch(ErrorException $e) {
    echo $e->getMessage();
}
?>

useraccess.php

<?php

session_start();

echo 'User is: ' . $fname . ' ' . $lname;

?>

1 个答案:

答案 0 :(得分:1)

在useraccess.php

中写下这个
<?php
  session_start();
  echo 'User is: ' . $_SESSION['admin']['fname'] . ' ' . $_SESSION['admin']['lname'];
?>