Php 会话数据ci3基本控制器

Php 会话数据ci3基本控制器,php,codeigniter,session,controller,base,Php,Codeigniter,Session,Controller,Base,我的控制器命名页面上有这个(我将它用作终端,以扩展到所有视图) 如您所见,我在会话数据上有一行“print\u r”,我得到了一些结果问题是它总是只返回“1” 这是我的基本控制器上的功能 class MY_Controller extends CI_Controller { public function __construct() { parent::__construct(); } public function is_logged_in()

我的控制器命名页面上有这个(我将它用作终端,以扩展到所有视图)

如您所见,我在会话数据上有一行“print\u r”,我得到了一些结果问题是它总是只返回“1”

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

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

    public function is_logged_in()
    {
        $user = $this->session->userdata();
        return isset($user);
    }  
}
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]',
    array('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]',
    array('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]',
    array('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 = array('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 = array('username' => $this->input->post('username'), 'password' => $this->input->post('password'));
        $result = $this->Account_model->login_account($data);
        $session_data = array('username' => $result['username'], 'email' => $result['email'], 'type' => $result['type']);
        $this->session->set_userdata('isloggedin',$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()
    {
        $user = $this->session->userdata();
        return isset($user);
    }  
}
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]',
    array('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]',
    array('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]',
    array('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 = array('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 = array('username' => $this->input->post('username'), 'password' => $this->input->post('password'));
        $result = $this->Account_model->login_account($data);
        $session_data = array('username' => $result['username'], 'email' => $result['email'], 'type' => $result['type']);
        $this->session->set_userdata('isloggedin',$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');}
}
现在有一些东西基本上是无用的,但我把它们放在那里供将来使用。就像$data['title']一样,由于我使用的是模板,所以它将用作每个视图头部标题的占位符

我做错什么了吗?为什么当我从我的控制器打印会话数据时,它只返回“1”


我一直在看指南,但大多数都已经过时了,我可能使用的是过去版本中不推荐的内容,如果有必要指出,我想练习干净整洁的编码。

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

print_r($this->session->all_userdata());
这部分呢

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

将返回1或0

您的方法不会以这种方式工作,因为userdata返回一个数组,其中至少有一个键名为\uuu ci\u last\u regenate(我不确定您使用的是哪个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,返回true或false

有关更多信息,请查看此处


很抱歉回复太迟了。设置另一个键是什么意思?你的意思是在我的会话中设置另一个数组_键?如“登录”为1或0或真或假?为了给您提供准确答案,您必须向我展示您的模型;)