douzhoubing2805 2014-03-13 04:59
浏览 61

使用谷歌进行OpenID登录并使用PHP的会话变量获取用户表单输入?

I'm trying to validate a user with LightOpenID and what I also need is to get their profile name but I'm having trouble getting a session variable $_SESSION['profile'] to get the form post $_POST['profile'] to store the information before the page is redirected to google. I'm not sure if this is the way to go about this but I can't think of any other way to tie a user to my database besides doing this. Here's the code

<?php

session_start(); 
# Logging in with Google accounts requires setting special identity, so this example shows how to do it.
require 'openid.php';
try 
{       
    $openid = new LightOpenID('http://www.some_site.com');
    if(!$openid->mode) {
        if(isset($_GET['login'])) 
        {          
            $_SESSION['profile'] = $_POST['profile'];
            $openid->identity = 'https://www.google.com/accounts/o8/id';
            header('Location: ' . $openid->authUrl());
        }
?>
<form action="?login" method="post">
        <table border="0"> 
         <tr><td colspan=2><h1>Login</h1></td></tr> 
         <tr><td>Username:</td><td> 
             <input type="text" name="profile" maxlength="40"> 
         </td></tr>             
        </table> 
    <button>Login with Google</button>
</form>
<?php
    } 
    elseif($openid->mode == 'cancel') 
    {
        echo 'User has canceled authentication!';
    } 
    else 
    {

        echo    $_SESSION['profile'];      
        echo 'User ' . ($openid->validate() ? $openid->identity .  ' has ' : 'has not ') . 'logged in.';          
    }
} 
catch(ErrorException $e) 
{
    echo $e->getMessage();
}

Doesn't show email after login though.

<?php
require 'openid.php';
$openid = new LightOpenID('http://www.some_site.com'); 

if (!$openid->mode) {
   $openid->identity = 'https://www.google.com/accounts/o8/id';
   $openid->required = array('contact/email');
   header('Location: ' . $openid->authUrl());
} else {
   if ($openid->validate()) {
      $attributes = $openid->getAttributes();
      $google_email = $attributes['contact/email'];
      echo $google_email."<br />";
      echo "<pre>" . print_r($_GET, true) . "</pre>";
      //header("Location: " . SITEURL);
   }
}
?>
  • 写回答

1条回答 默认 最新

  • dongzhimin2231 2014-03-13 05:11
    关注

    Try with

    <?php
    if (!$openid->mode) {
       $openid->identity = 'https://www.google.com/accounts/o8/id';
       $openid->required = array('contact/email');
       header('Location: ' . $openid->authUrl());
    } else {
       if ($openid->validate()) {
          $attributes = $openid->getAttributes();
          $google_email = $attributes['contact/email'];
          echo $google_email."<br />";
          echo "<pre>" . print_r($_GET, true) . "</pre>";
          //header("Location: " . SITEURL);
       }
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计