0

我已将我的网站路由为管理员版主。意味着当我使用 Auth 组件时,我的会话被创建为Auth->Admin->id&Auth->Moderator->id并且以Auth->User->id这种方式。

现在我尝试$this->Auth->user了返回null的函数,为什么?

据我所知$this->Auth->user返回会话。

我知道 and else 的传统方式,$this->Session->reads('Auth.Admin')但我想使用 Auth->user 函数为什么它不起作用

4

2 回答 2

1

请使用它来获取用户会话数据。

public function login() {
 if ($this->request->is('post')) {
     if ($this->Auth->login()) {
    $this->redirect($this->Auth->redirect());
     } else {
    $this->Session->setFlash(__('Invalid username or password, try again'));
     }
 }
}

public function index() {
    pr($this->Auth->User());
}
于 2013-11-01T11:40:41.683 回答
0
        auth_helper:<?php
        defined('BASEPATH') OR exit('No direct script access allowed');

        if ( ! function_exists('auth'))
        {
            function auth()
            {
                $CI =& get_instance();
                $CI->load->model('Auth_model');
                return $CI->Auth_model;
            }

            function Adminauth($admin_id) 
            {
                if($admin_id == false)
                {
                    redirect('login');
                }
            }
        }
    auth_model:
    <?php
    if (!defined('BASEPATH')) {
        exit('No direct script access allowed');
    }

    class Auth_model extends CI_Model {
        private $user = null;
        private $ci = null;

        public function __construct() {
            parent::__construct();
            $CI =& get_instance();
            $this->ci = $CI;
        }

        public function get($name, $default = null){
            if ($this->ci->session->has_userdata('auth.Adminuser_id')) {
                 $user_id = $this->ci->session->userdata('auth.Adminuser_id');
                 $this->user = $this->db->query("SELECT * FROM members WHERE u_id=? LIMIT 1", array($user_id))->row();
            }

            return !empty($this->user) ? $this->user->u_name : $default;
        }
        public function is_logged(){
            return $this->ci->session->has_userdata('auth.Adminuser_id');
        }
        public function login($user_id){
            return $this->ci->session->set_userdata('auth.Adminuser_id', $user_id);
        }
        public function logout(){
            return $this->ci->session->unset_userdata('auth.Adminuser_id');
        }
    }
于 2016-10-25T09:00:37.993 回答