Session数据ci3基地控制器

Session data ci3 base controller

我在名为 pages 的控制器上有这个(我用它作为终端来分支到所有视图)

class pages extends MY_Controller 
{
    public function verification()
    {
        $session_data = $this->is_logged_in();

        print_r($session_data);

        if($this->is_logged_in())
        {

        }
    }
}

如您所见,我在 session_data 上有一行 'print_r',我得到了一些结果,但问题是它总是 returns 只有“1”。

这是我的基本控制器上的功能。

class MY_Controller extends CI_Controller 
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function is_logged_in()
    {
        $user = $this->session->userdata();
        return isset($user);
    }  
}

这是登录后的代码块,在我的身份验证器控制器上创建 session 数据。

Class user_authentication extends MY_Controller 
{
    public function register()
    {
        $this->form_validation->set_rules('username', 'Username','required|trim|min_length[8]|max_length[24]|is_unique[user.username]', [
            'required' => 'You have not provided %s.', 
            'is_unique' => 'This %s already exists.'
        ]);    
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[8]|max_length[16]');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|min_length[6]|max_length[30]|is_unique[user.email]', [
            'required' => 'You have not provided %s. ', 
            'is_unique' => 'This %s already exists.'
        ]);
            $this->form_validation->set_rules('type', 'Account Type', 'trim|required|min_length[1]|max_length[1]', [
            'required' => 'please select: 1 - normal, 2 - trainer'
        ]);
        
        $this->form_validation->set_error_delimiters('', '');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('templates/header');
            $this->load->view('templates/navigation');
            $this->load->view('pages/login');
            $this->load->view('templates/footer');
        }
        else
        {
            $data = [
                'username' => $this->input->post('username'),
                'password' => $this->input->post('password'),
                'email' => $this->input->post('email'),
                'type' => $this->input->post('type'),
            ]; 
            
            $result = $this->Account_model->create_account($data);

            if($result == true)
            {
                $data['message_display'] = 'Registration Successful';

                $this->load->view('templates/header');
                $this->load->view('templates/navigation');
                $this->load->view('pages/homepage',$data);
                $this->load->view('templates/footer');    
            }
            else
            {
                $data['message_display'] = 'Username and/or Email already exists';

                $this->load->view('templates/header');
                $this->load->view('templates/navigation');
                $this->load->view('pages/login',$data);
                $this->load->view('templates/footer');
            }
        }
    }
    
    public function login()
    {
        $this->form_validation->set_rules('username', 'Username', 'required|trim|min_length[8]|max_length[24]');
        $this->form_validation->set_rules('password', 'Password', 'required|trim|min_length[8]|max_length[16]');

        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('templates/header');
            $this->load->view('templates/navigation');
            $this->load->view('pages/login');
            $this->load->view('templates/footer');
        }
        else
        {
            $data = [
                'username' => $this->input->post('username'), 
                'password' => $this->input->post('password')
            ];
            $result = $this->Account_model->login_account($data);
            
            $this->session->set_userdata('isloggedin', [
                'username' => $result['username'], 
                'email' => $result['email'], 
                'type' => $result['type']
            ]);

            $data['title'] = 'home';

            $this->load->view('templates/header');
            $this->load->view('templates/navigation');
            $this->load->view('pages/home');
            $this->load->view('templates/footer');
        }
    }
}

有些东西现在基本上没用,但我把它们放在那里以备将来使用。就像 $data['title'] 因为我使用的是模板,所以会用它作为每个视图头部标题的占位符。

我做错了什么吗?为什么当我 print_r 来自 MY_Controller 的 session 数据时它只有 returns '1'.

我一直在查看指南,但其中大部分都已过时,我可能正在使用过去版本中已弃用的内容,如果请指出它们,我想练习整洁干净的编码。

如果您想打印所有会话数据,请使用此

print_r($this->session->all_userdata());

这部分

public function is_logged_in()
{
    $user = $this->session->userdata();
    return isset($user);
}  

将 return 1 或 0;

你的方法不会那样工作,因为 userdata returns 一个数组,其中至少有一个名为 __ci_last_regenerate 的键(我不确定是哪个CI 您使用的版本,但如果您自动加载会话库,您总是会在此处获得结果)

设置一个额外的密钥 isLoggedin 就可以了。

您的登录函数:

public function login()
{
    $this->form_validation->set_rules('username', 'Username', 'required|trim|min_length[8]|max_length[24]');
    $this->form_validation->set_rules('password', 'Password', 'required|trim|min_length[8]|max_length[16]');
    if($this->form_validation->run() == FALSE)
    {
        $this->load->view('templates/header');
        $this->load->view('templates/navigation');
        $this->load->view('pages/login');
        $this->load->view('templates/footer');}
    else
    {
        $data = array('username' => $this->input->post('username'), 'password' => $this->input->post('password'));
        $result = $this->Account_model->login_account($data);

        if ($result)
        {
            $session_data = array('username' => $result['username'], 'email' => $result['email'], 'type' => $result['type'], 'isLoggedin' => true);
            $this->session->set_userdata($session_data);

            $data['title'] = 'home';
            $this->load->view('templates/header');
            $this->load->view('templates/navigation');
            $this->load->view('pages/home');
            $this->load->view('templates/footer');

        }

    }
}

和您的控制器

class MY_Controller extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
    }

    public function is_logged_in()
    {
        return $this->session->userdata('isLoggedin');
    }  
}

通过这种方法,您可以得到正确的值或 NULL,returns 真或假;

For more information you can take a look here https://codeigniter.com/userguide3/libraries/sessions.html#CI_Session::userdata