五月 18th, 2009基于OpenID Enabled修改而成的openid类。
此文章来源于后羿之弓,转载请注明出处
基于OpenID Enabled修改而成的openid类,因为是自己用,所以不怎么规范。凑合着用吧。废话不多说,代码奉上
- <?
- /**
- * openid处理类
- * @author 周于<zhouyuhi@gmail.com>
- * @version 1.0
- */
- $path = ini_get('include_path');
- $path = PHP_LIB . PATH_SEPARATOR . $path;//PHP_LIB宏根据自己的实际情况而定
- ini_set('include_path', $path);
- session_start();
- require_once "Auth/OpenID/Consumer.php";
- require_once "Auth/OpenID/FileStore.php";
- require_once "Auth/OpenID/SReg.php";
- require_once "Auth/OpenID/PAPE.php";
- class openid{
- var $openid;
- function __construct($openid=''){
- if(!empty($openid)){
- $this->openid = trim($openid);
- }
- }
- function getStore() {
- $store_path = "/tmp/_php_consumer_test";
- if (!file_exists($store_path) &&!mkdir($store_path)) {
- die("创建文件夹失败 '$store_path'. ");
- }
- return new Auth_OpenID_FileStore($store_path);
- }
- function getConsumer() {
- $store = $this->getStore();
- $consumer =& new Auth_OpenID_Consumer($store);
- return $consumer;
- }
- function getScheme() {
- $scheme = 'http';
- if (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] == 'on') {
- $scheme .= 's';
- }
- return $scheme;
- }
- function getTrustRoot() {
- return sprintf("%s://%s:%s%s/",$this->getScheme(), $_SERVER['SERVER_NAME'],$_SERVER['SERVER_PORT'],dirname($_SERVER['PHP_SELF']));
- }
- function getReturnTo() {
- return sprintf("%s://%s:%s%s/?module=user&act=login&do=openid",$this->getScheme(), $_SERVER['SERVER_NAME'],$_SERVER['SERVER_PORT'],dirname($_SERVER['PHP_SELF']));
- }
- function doRequest() {
- $consumer = $this->getConsumer();
- $auth_request = $consumer->begin($this->openid);
- if (!$auth_request) {
- die("无效的OpenID.");
- }
- $sreg_request = Auth_OpenID_SRegRequest::build(
- // Required
- array('nickname'),
- // Optional
- array('fullname', 'email'));
- if ($sreg_request) {
- $auth_request->addExtension($sreg_request);
- }
- $policy_uris =array('http://schemas.openid.net/pape/policies/2007/06/multi-factor-physical',
- 'http://schemas.openid.net/pape/policies/2007/06/multi-factor',
- 'http://schemas.openid.net/pape/policies/2007/06/phishing-resistant');
- $pape_request = new Auth_OpenID_PAPE_Request($policy_uris);
- if ($pape_request) {
- $auth_request->addExtension($pape_request);
- }
- if ($auth_request->shouldSendRedirect()) {
- $redirect_url = $auth_request->redirectURL($this->getTrustRoot(),$this->getReturnTo());
- if (Auth_OpenID::isFailure($redirect_url)) {
- die("不能定位到服务器: " . $redirect_url->message);
- } else {
- header("Location: ".$redirect_url);
- }
- } else {
- $form_id = 'openid_message';
- $form_html = $auth_request->htmlMarkup($this->getTrustRoot(), $this->getReturnTo(),
- false, array('id' => $form_id));
- if (Auth_OpenID::isFailure($form_html)) {
- displayError("不能定位到服务器: " . $form_html->message);
- } else {
- print $form_html;
- }
- }
- }
- function doResponse(){
- $consumer = $this->getConsumer();
- $return_to = $this->getReturnTo();
- $response = $consumer->complete($return_to);
- if ($response->status == Auth_OpenID_CANCEL) {
- $this->openid = '验证取消';
- return false;
- } else if ($response->status == Auth_OpenID_FAILURE) {
- $this->openid = "OpenID验证失败: " . $response->message;
- return false;
- } else if ($response->status == Auth_OpenID_SUCCESS) {
- $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
- $sreg = $sreg_resp->contents();
- $this->openid = array('email'=>$sreg['email'],'nickname'=>$sreg['nickname'],'fullname'=>$sreg['fullname']);
- return true;
- }
- }
- function getResult(){
- return $this->openid;
- }
- }
- ?>
再写一个demo
a.发出验证
- $openid = new openid($user);
- $openid->doRequest();
b.得到结果
- $openid = new openid();
- if(!$openid->doResponse()){
- echo '登录失败';var_dump($openid->getResult());exit;
- }
- echo '登录成功!返回内容如下:';var_dump($openid->getResult());
over.