Home > php教程 > PHP源码 > body text

在 codeigniter 中通过 ucenter 与其他系统双向登录退出

PHP中文网
Release: 2016-05-26 08:20:06
Original
1043 people have browsed it

    codeigniter 中无用户系统

uc_client.config.php

<?php
define(&#39;UC_CONNECT&#39;, &#39;mysql&#39;);
define(&#39;UC_DBHOST&#39;, &#39;localhost&#39;);
define(&#39;UC_DBUSER&#39;, &#39;root&#39;);
define(&#39;UC_DBPW&#39;, &#39;root&#39;);
define(&#39;UC_DBNAME&#39;, &#39;ucenter&#39;);
define(&#39;UC_DBCHARSET&#39;, &#39;utf8&#39;);
define(&#39;UC_DBTABLEPRE&#39;, &#39;`ucenter`.uc_&#39;);
define(&#39;UC_DBCONNECT&#39;, &#39;0&#39;);
define(&#39;UC_KEY&#39;, &#39;aaaaaaa&#39;);
define(&#39;UC_API&#39;, &#39;http://localhost/project/ucenter&#39;);
define(&#39;UC_CHARSET&#39;, &#39;utf-8&#39;);
define(&#39;UC_IP&#39;, &#39;&#39;);
define(&#39;UC_APPID&#39;, &#39;2&#39;);
define(&#39;UC_PPP&#39;, &#39;20&#39;);
?>
Copy after login


uc.php

<?php
 
define(&#39;API_SYNLOGIN&#39;, 1);
define(&#39;API_SYNLOGOUT&#39;, 1);
define(&#39;API_RETURN_FAILED&#39;, &#39;-1&#39;);
define(&#39;API_RETURN_FORBIDDEN&#39;, &#39;-2&#39;);
 
define(&#39;APIROOT&#39;, dirname(__FILE__));
define(&#39;BASEROOT&#39;, dirname(APIROOT));
define(&#39;S_ROOT&#39;, BASEROOT.&#39;/application/libraries/lib/class/&#39;);
define("DS", DIRECTORY_SEPARATOR);
define(&#39;BASEPATH&#39;, 1);
 
function import($filepath, $base = null, $key = null)
{
    static $paths;
    $keypath = $key ? $key.$filepath : $filepath;
 
    if(!isset($paths[$keypath])) {
        if(is_null($base)) {
            $base = BASEROOT.&#39;/application/libraries/lib/&#39;;
        }
        $parts = explode(&#39;.&#39;, $filepath);
        array_pop($parts);
        $path = str_replace(&#39;.&#39;, DS, $filepath);
        $paths[$keypath] = include $base.$path.&#39;.php&#39;;
    }
     
    return $paths[$keypath];
}
 
import(&#39;func.common&#39;);
 
error_reporting(0);
set_magic_quotes_runtime(0);
 
defined(&#39;MAGIC_QUOTES_GPC&#39;) || define(&#39;MAGIC_QUOTES_GPC&#39;, get_magic_quotes_gpc());
 
load_ucenter();
 
$get = $post = array();
 
$code = @$_GET[&#39;code&#39;];
 
parse_str(uc_authcode($code, &#39;DECODE&#39;, UC_KEY), $get);
 
if(MAGIC_QUOTES_GPC) {
    $get = uc_stripslashes($get);
}
 
$timestamp = time();
if($timestamp - $get[&#39;time&#39;] > 3600) {
    exit(&#39;Authracation has expiried&#39;);
}
if(empty($get)) {
    exit(&#39;Invalid Request&#39;);
}
 
include_once S_ROOT.&#39;./uc_client/lib/xml.class.php&#39;;
$post = xml_unserialize(file_get_contents(&#39;php://input&#39;));
if(in_array($get[&#39;action&#39;], array(&#39;synlogin&#39;, &#39;synlogout&#39;))) {
    $uc_note = new uc_note();
    echo $uc_note->$get[&#39;action&#39;]($get, $post);
    exit();
} else {
    exit(API_RETURN_FAILED);
}
 
 
function ssetcookie($var, $value, $life = 0, $prefix = 0)
{
    global $cookiepre, $cookiedomain, $cookiepath, $timestamp, $_SERVER;
    setcookie(($prefix ? $cookiepre : &#39;&#39;).$var, $value, $life ? $timestamp + $life : 0, &#39;/&#39;, "", $_SERVER[&#39;SERVER_PORT&#39;] == 443 ? 1 : 0);
}
 
class uc_note
{
 
    function synlogin($get, $post)
    {
 
        if(!API_SYNLOGIN) {
            return API_RETURN_FORBIDDEN;
        }
     
        header(&#39;P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"&#39;);
 
        $cookietime = 31536000;
        $uid = intval($get[&#39;uid&#39;]);
        $username = $get[&#39;username&#39;];
        ssetcookie(&#39;ruishang_auth&#39;, uc_authcode("$uid\t$username", &#39;ENCODE&#39;), $cookietime);
        ssetcookie(&#39;ruishang_loginuser&#39;, $username, $cookietime);
    }
     
    function synlogout($get, $post)
    {
         
        if(!API_SYNLOGOUT) {
            return API_RETURN_FORBIDDEN;
        }
 
        header(&#39;P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"&#39;);
        ssetcookie(&#39;ruishang_auth&#39;, &#39;&#39;, -86400 * 365);
        ssetcookie(&#39;ruishang_loginuser&#39;, &#39;&#39;, -86400 * 365);
    }
     
}
?>
Copy after login


user.php

<?php if ( ! defined(&#39;BASEPATH&#39;)) exit(&#39;No direct script access allowed&#39;);
 
define("DS", DIRECTORY_SEPARATOR);
 
function import($filepath, $base = null, $key = null)
{
    static $paths;
    $keypath = $key ? $key.$filepath : $filepath;
 
    if(!isset($paths[$keypath])) {
        if(is_null($base)) {
            $base = APPPATH.&#39;libraries/lib/&#39;;
        }
        $parts = explode(&#39;.&#39;, $filepath);
        array_pop($parts);
        $path = str_replace(&#39;.&#39;, DS, $filepath);
        $paths[$keypath] = include $base.$path.&#39;.php&#39;;
    }
     
    return $paths[$keypath];
}
 
function load_ucenter()
{
    import(&#39;class.uc_client.config&#39;);
    import(&#39;class.uc_client.client&#39;);
}
 
class User extends MY_Controller
{
 
    public function __construct()
    {
        parent::__construct();
    }
 
    public function login()
    {
        load_ucenter();
        $this->load->helper(&#39;cookie&#39;);
 
        $_POST[&#39;username&#39;] = &#39;admin&#39;;
        $_POST[&#39;password&#39;] = &#39;admin&#39;;
        $username = $this->input->post(&#39;username&#39;);
        $password = $this->input->post(&#39;password&#39;);
 
        // 登录并获取信息
        list($uid, $username, $password, $email) = uc_user_login($username, $password);
   
        if($uid > 0) {
            $cookie = array(
                &#39;name&#39;   => &#39;ruishang_auth&#39;,
                &#39;value&#39;  => uc_authcode($uid."\t".$username, &#39;ENCODE&#39;, UC_KEY),
                &#39;expire&#39; => &#39;86500&#39;,
            );
 
            // 生成站点的cookies,名称应当是主站的cookies名称,加密时的格式请注意
            set_cookie($cookie);
 
            // 这是同步登录其它应用的代码,输出为js代码,一定要输出到屏幕中一次
            $ret = uc_user_synlogin($uid);
        } elseif($uid == -1) {
            echo &#39;用户不存在,或者被删除&#39;;
        } elseif($uid == -2) {
            echo &#39;密码错误&#39;;
        } else {
            echo &#39;未定义的错误&#39;;
        }
 
        echo($ret);
    }
}
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!