欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

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

程序员文章站 2022-05-07 09:13:40
...
codeigniter 中无用户系统

uc_client.config.php

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


uc.php

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


user.php

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