此文章来源于后羿之弓,转载请注明出处

基于OpenID Enabled修改而成的openid类,因为是自己用,所以不怎么规范。凑合着用吧。废话不多说,代码奉上

  1. <?
  2. /**
  3. * openid处理类
  4. * @author 周于<zhouyuhi@gmail.com>
  5. * @version 1.0
  6. */
  7. $path = ini_get('include_path');
  8. $path = PHP_LIB . PATH_SEPARATOR . $path;//PHP_LIB宏根据自己的实际情况而定
  9. ini_set('include_path', $path);
  10. session_start();
  11.  
  12. require_once "Auth/OpenID/Consumer.php";
  13. require_once "Auth/OpenID/FileStore.php";
  14. require_once "Auth/OpenID/SReg.php";
  15. require_once "Auth/OpenID/PAPE.php";
  16. class openid{
  17.     var $openid;
  18.     function __construct($openid=''){
  19.         if(!empty($openid)){
  20.             $this->openid = trim($openid);
  21.         }
  22.     }
  23.  
  24.     function getStore() {
  25.         $store_path = "/tmp/_php_consumer_test";
  26.  
  27.         if (!file_exists($store_path) &&!mkdir($store_path)) {
  28.             die("创建文件夹失败 '$store_path'. ");
  29.         }
  30.         return new Auth_OpenID_FileStore($store_path);
  31.     }
  32.  
  33.     function getConsumer() {
  34.         $store = $this->getStore();
  35.         $consumer =& new Auth_OpenID_Consumer($store);
  36.         return $consumer;
  37.     }
  38.     function getScheme() {
  39.         $scheme = 'http';
  40.         if (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] == 'on') {
  41.             $scheme .= 's';
  42.         }
  43.         return $scheme;
  44.     }
  45.  
  46.     function getTrustRoot() {
  47.         return sprintf("%s://%s:%s%s/",$this->getScheme(), $_SERVER['SERVER_NAME'],$_SERVER['SERVER_PORT'],dirname($_SERVER['PHP_SELF']));
  48.     }
  49.     function getReturnTo() {
  50.         return sprintf("%s://%s:%s%s/?module=user&act=login&do=openid",$this->getScheme(), $_SERVER['SERVER_NAME'],$_SERVER['SERVER_PORT'],dirname($_SERVER['PHP_SELF']));
  51.     }
  52.    
  53.     function doRequest() {
  54.         $consumer = $this->getConsumer();
  55.         $auth_request = $consumer->begin($this->openid);
  56.  
  57.         if (!$auth_request) {
  58.             die("无效的OpenID.");
  59.         }
  60.  
  61.         $sreg_request = Auth_OpenID_SRegRequest::build(
  62.         // Required
  63.         array('nickname'),
  64.         // Optional
  65.         array('fullname', 'email'));
  66.  
  67.         if ($sreg_request) {
  68.             $auth_request->addExtension($sreg_request);
  69.         }
  70.  
  71.         $policy_uris =array('http://schemas.openid.net/pape/policies/2007/06/multi-factor-physical',
  72.                           'http://schemas.openid.net/pape/policies/2007/06/multi-factor',
  73.                           'http://schemas.openid.net/pape/policies/2007/06/phishing-resistant');
  74.        
  75.         $pape_request = new Auth_OpenID_PAPE_Request($policy_uris);
  76.         if ($pape_request) {
  77.             $auth_request->addExtension($pape_request);
  78.         }
  79.         if ($auth_request->shouldSendRedirect()) {
  80.             $redirect_url = $auth_request->redirectURL($this->getTrustRoot(),$this->getReturnTo());
  81.             if (Auth_OpenID::isFailure($redirect_url)) {
  82.                 die("不能定位到服务器: " . $redirect_url->message);
  83.             } else {
  84.                 header("Location: ".$redirect_url);
  85.             }
  86.         } else {
  87.             $form_id = 'openid_message';
  88.             $form_html = $auth_request->htmlMarkup($this->getTrustRoot(), $this->getReturnTo(),
  89.             false, array('id' => $form_id));
  90.             if (Auth_OpenID::isFailure($form_html)) {
  91.                 displayError("不能定位到服务器: " . $form_html->message);
  92.             } else {
  93.                 print $form_html;
  94.             }
  95.         }
  96.     }
  97.     function doResponse(){
  98.         $consumer = $this->getConsumer();
  99.         $return_to = $this->getReturnTo();
  100.         $response = $consumer->complete($return_to);
  101.  
  102.         if ($response->status == Auth_OpenID_CANCEL) {
  103.             $this->openid = '验证取消';
  104.             return false;
  105.         } else if ($response->status == Auth_OpenID_FAILURE) {
  106.             $this->openid = "OpenID验证失败: " . $response->message;
  107.             return false;
  108.         } else if ($response->status == Auth_OpenID_SUCCESS) {
  109.             $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
  110.             $sreg = $sreg_resp->contents();
  111.             $this->openid = array('email'=>$sreg['email'],'nickname'=>$sreg['nickname'],'fullname'=>$sreg['fullname']);
  112.             return true;
  113.         }
  114.     }
  115.     function getResult(){
  116.         return $this->openid;
  117.     }
  118. }
  119. ?>

再写一个demo
a.发出验证

  1. $openid = new openid($user);
  2. $openid->doRequest();

b.得到结果

  1. $openid = new openid();
  2. if(!$openid->doResponse()){
  3.     echo '登录失败';var_dump($openid->getResult());exit;
  4. }
  5. echo '登录成功!返回内容如下:';var_dump($openid->getResult());

over.