• ucenter接口


    1,首先是进入ucenter后台,建立网站,完成各种通讯操作,最后达到通讯成功

    各种通讯不成功,网上很多检查点,此处略..

    2,通讯成功后,以ci框架为例子

    将官网的api包下载下来,其中一个api目录,里面有个uc.php文件 直接放在根目录 ,方便引用

    3,ci里面就开始引用了

    比如建立一个index控制器,里面各种方法

    我目前将涉及到ucenter的操作都写在一个libray里面,方便控制器调用,但写得不够好,先mark吧,逐步完善

    libarary uc.php 说白了就是写一个类,到时候控制器里面调用这个类来操作

    <?php
    class Uc{
        
        public $Example_uid='';
        public $Example_username='';
        public $username='';
        //library应该只写逻辑,不涉及到post操作
        function __construct(){
            $ci =& get_instance();
            //这里要引用根目录下uc文件,路径比较2
            require_once  APPPATH.'../../config.inc.php';
            require_once  APPPATH.'../../uc_client/client.php';
            $this->get_uid();
        }
        
        public function get_uid(){
            /**
             * 获取当前用户的 UID 和 用户名
             * Cookie 解密直接用 uc_authcode 函数,用户使用自己的函数
             * 此函数方便后面使用,很多函数里面需要uid
             */
            if(!empty($_COOKIE['Example_auth'])) {
                list($Example_uid, $Example_username) = explode("	", uc_authcode($_COOKIE['Example_auth'], 'DECODE'));
            } else {
                $Example_uid = $Example_username = '';
            }
            $this->Example_uid=$Example_uid;
            $this->Example_username=$Example_username;
            
        }
    
        public function login(){
            //直接登录
            list($uid, $username, $password, $email) = uc_user_login($_POST['username'], $_POST['password']);
            setcookie('Example_auth', '', -86400);
            if($uid > 0){    
            
            //用户登陆成功,设置 Cookie,加密直接用 uc_authcode 函数,用户使用自己的函数
            setcookie('Example_auth', uc_authcode($uid."	".$username, 'ENCODE'));
            $this->username=$username;//用户激活的时候要用
            //生成同步登录的代码
            $ucsynlogin = uc_user_synlogin($uid);
    
            //登录成功了,提示登录成功
                    echo "{$username}登录成功";
                    return TRUE;
            }
                elseif($uid == -1) {
                    echo '用户不存在,或者被删除';
                    return FALSE;
                } elseif($uid == -2) {
                    echo '密码错';
                    return FALSE;
                } else {
                    echo '未定义';
                    return FALSE;
                }
        }
        
        public function logout(){
        //退出登录
            setcookie('Example_auth', '', -86400);
            //生成同步退出的代码
            $ucsynlogout = uc_user_synlogout();
            return $ucsynlogout;
        }
        
        
        public function get_avatar($type='big'){
            //获取头像 三个参数,big,middle,small,change
            $Example_uid=$this->Example_uid;
    
            switch ($type){
                case 'big' :
                    echo '<img src="'.UC_API.'/avatar.php?uid='.$Example_uid.'&size=big">';
                     break;
                case 'middle' :
                    echo '<img src="'.UC_API.'/avatar.php?uid='.$Example_uid.'&size=middle">';
                    break;
                case 'small' :
                    echo '<img src="'.UC_API.'/avatar.php?uid='.$Example_uid.'&size=small">';
                    break;
                case 'change':
                    echo uc_avatar($Example_uid);
                default:break;
            }
        
        }
        
        public function reg(){
            //在UCenter注册用户信息
            $uid = uc_user_register($_POST['username'], $_POST['password'], $_POST['email']);
            if($uid <= 0) {
                if($uid == -1) {
                    echo '用户名不合法';
                } elseif($uid == -2) {
                    echo '包含要允许注册的词语';
                } elseif($uid == -3) {
                    echo '用户名已经存在';
                } elseif($uid == -4) {
                    echo 'Email 格式有误';
                } elseif($uid == -5) {
                    echo 'Email 不允许注册';
                } elseif($uid == -6) {
                    echo '该 Email 已经被注册';
                } else {
                    echo '未定义';
                }
            }else {
                $username = $_POST['username'];
            }if($username) {
                $db2=$this->load->database('uc',TRUE);
                $data['uid']=$uid;
                $data['username']=$username;
                $data['admin']=0;
                $db2->insert('members',$data);
        
                //注册成功,设置 Cookie,加密直接用 uc_authcode 函数,用户使用自己的函数
                setcookie('Example_auth', uc_authcode($uid."	".$username, 'ENCODE'));
                echo '注册成功<br>';
                exit;
            }
            
        }
        
    
    }

    控制器的操作

    <?php
    class Index extends CI_Controller{
        public function show(){
            //显示登陆界面
            $this->load->view('index_login');    
        
        }
        
        public function login(){
            //从uc登陆,登陆后的处理
            if($this->input->post('submit')){
                header("Content-type:text/html;charset=utf-8");
                $this->load->library('uc');
                if($this->uc->login()){
                    
                    //此时说明在uc里面查询到了,开始在自己数据库里面查询,如果没有查询到,提示激活
                    $this->load->model('index_m');
                
                    $data['username']=$this->input->post('username');
                    $data['password']=md5($this->input->post('password'));
                
                    if(!$this->index_m->login($data)){
                        //没查到 ,跳到激活界面
                        redirect('index/activation');
                    }
                        
                $this->uc->get_avatar('small');
                $this->uc->get_avatar('middle');
                $this->uc->get_avatar('big');
            
                echo "<br><br>";
                $this->uc->get_avatar('change');
                echo "<a href='".site_url('index/logout')."'>退出</a>";
                
                //激活
                
                
                }
            }
        }
    
        
        public function logout(){
            header("Content-type:text/html;charset=utf-8");
                $this->load->library('uc');
                $logout=$this->uc->logout();
                echo $logout."<br><a href='".site_url('index/show')."'>返回</a>";
        }
        
        public function reg(){
            $this->load->view('index_reg');
        }
        
        public function do_reg(){
            header("Content-type:text/html;charset=utf-8");
            $this->load->library('uc');
            $this->uc->reg();
        }
        
        //先要第一步将原来是数据库的东西导入到ucenter
        private function import(){
            $res=$this->db->get('user');
            $res=$res->result_array();
            
            $db2=$this->load->database('uc',TRUE);
            //需要插入到uc表的有username,password=>salt,regtime,
            foreach ($res as $r){
                $data['username']=$r['username'];
                $data['salt']=$salt = substr(uniqid(rand()), -6);
                $data['password']= md5($r['password'].$salt);
                $data['regdate']=$r['regtime'];
                $db2->insert('members',$data);
                echo $db2->last_query();
            }
        }
    
    }
  • 相关阅读:
    css flex布局实现后台页面
    html5 css iframe实现后台框架,仅用于学习案例
    nginx 多个网站配置
    nginx 负载 访问时 去掉端口
    nginx 负载
    解标准数独算法
    C++ execute linux cmd and retrieve the output
    C++ generate in Ubuntu
    shell操作典型案例--FTP操作
    PHP7 新写法
  • 原文地址:https://www.cnblogs.com/dk1988/p/3664456.html
Copyright © 2020-2023  润新知