首页 > php教程 > PHP源码 > 正文

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

PHP中文网
发布: 2016-05-26 08:20:06
原创
1516人浏览过

    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);
    }
     
}
?>
登录后复制


ghiblitattoo
ghiblitattoo

用AI创造独特的吉卜力纹身

ghiblitattoo 175
查看详情 ghiblitattoo

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);
    }
}
登录后复制


最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号