doujia1679 2017-01-09 01:25
浏览 84
已采纳

会话数据ci3基本控制器

I have this on my controller named pages(i use it as a terminal to branch out to all views)

    class pages extends MY_Controller {
    public function verification(){

    $session_data = $this->is_logged_in();
    print_r($session_data);
        if($this->is_logged_in()){


        }
    }
}

As you can see, i have a line 'print_r' on the session_data, i got some results the problem is it always returns only '1'.

This is the function on my base controller.

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

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

And this is the code block after logging in creating the session data on my authenticator controller.

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');}
}

There are some stuff that are basically useless as of right now but i'm putting them there for future uses. like the $data['title'] gonna use it as a placeholder for the title on the head of each view since i am using templates.

Am i doing something wrong? Why does when i print_r the session data from MY_Controller it only returns '1'.

I've been looking at guides but most of them are outdated and i am probably using deprecated stuff from past versions, if so kindly point them out, i want to practice coding neat and clean.

  • 写回答

2条回答 默认 最新

  • duanhaodi4809 2017-01-09 12:00
    关注

    Your approach won't work that way because userdata returns an array with at least one key called __ci_last_regenerate (i'm not sure which CI version you use but if you autoload the session library you would always get an result here)

    Set an extra key isLoggedin and you'll be fine.

    Your login function:

    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');
    
            }
    
        }
    }
    

    and your controller

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

    With this approach you get either a correct value or NULL which returns true or false;

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器