【问题标题】:Page Redirecting and storing OpenID user information页面重定向和存储 OpenID 用户信息
【发布时间】:2013-11-08 05:22:37
【问题描述】:

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

登录.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;

?>

【问题讨论】:

    标签: php redirect openid


    【解决方案1】:

    把这个写在useraccess.php中

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

    【讨论】:

    • +1 谢谢!如果用户未登录,是否可以将用户重定向到登录页面?
    • 你写了用户登录后重定向到useraccess.php的代码
    • 是的,我的意思是如果有人在浏览器中输入网址:mydomain.com/useraccess.php我希望他们重定向到mydomain.com/login.php如果没有登录
    • 设置条件来检查会话是否设置。 检查会话的条件是否设置。
    • 类似if(!isset($_SESSION['admin']['fname'])) 或者需要更通用的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 2016-05-06
    • 2013-01-20
    • 2010-12-05
    • 2017-04-21
    相关资源
    最近更新 更多