【问题标题】:CodeIgniter Session is not set (trying to set session in callback function)CodeIgniter 会话未设置(试图在回调函数中设置会话)
【发布时间】:2020-01-01 16:53:54
【问题描述】:

在这种情况下没有设置会话。它在另一个项目中对我来说很早,但现在我遇到了问题

这是我的控制器:

class Login extends CI_Controller {

    public $username;
    public $status;
    public $user_id;
    public $type = 0;

    function index() {
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->helper(array('form', 'url'));
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('username', 'Username', 'required|callback_check_details');

        $this->load->library('form_validation');

        if ($this->session->userdata('loggedin') == 1) {//already done
            redirect('/Exams');
        } else if ($this->form_validation->run() == FALSE) {
            $this->load->view('Login_form');
        } else {

            if (isset($_REQUEST['redirect']) && $_REQUEST['redirect'] != "") {//anyone wants to get back            
                redirect($_REQUEST['redirect']);
            } else {
                redirect('/Exams'); //otherwise
            }
        }
    }

    function check_details() {
        $username = $this->db->escape($this->input->post('username'));
        $this->username = $username;
        $password = $this->input->post('password');

        $query = $this->db->query("select *  from users where username=$username or email=$username");
        if ($query->num_rows() > 0) {
            $row = $query->row();
            if ($row->active == 0 || $row->active == 2) {
                $this->form_validation->set_message('check_details', 'Your account has not activated yet. Please Check your email');
                return FALSE;
            }
            if ($this->bcrypt->check_password($password, $row->password)) {

                $this->user_id = $row->user_id;
                $this->type = $row->type;
                $this->status = 1;
                $this->session->set_userdata('loggedin', 1);
                $this->session->set_userdata('username', $this->username);
                $this->session->set_userdata('user_id', $this->user_id);
                $this->session->set_userdata('type', $this->type);
                $this->session->set_userdata('full_name', $row->full_name);
                $this->session->set_userdata('roll_number', $row->roll_number);
                $this->session->set_userdata('phone_number', $row->phone_number);
                $this->session->set_userdata('profile_picture', $row->profile_picture);
                $this->session->set_userdata('level', $this->permissions->get_level());

                $this->logger->insert('Logged in.', TRUE, TRUE);
                return TRUE;
            }
        }
        $this->form_validation->set_message('check_details', 'The username or password is incorrect ');
        return FALSE;
    }

}

当我在 check_detail 函数中检查会话时,它总是打印 会话已设置

if($this->session->userdata('loggedin')!=true){
    echo "session is not set";
} else {
    echo "session is set";
}

但是当我签入索引时它总是打印 session is not set 我也尝试在索引函数中设置会话但仍然面临同样的问题

【问题讨论】:

    标签: php codeigniter session


    【解决方案1】:

    您也应该在 autoload.php 中包含会话库或在 check_details 函数中包含会话库。试试这个代码

    <?php
    class Login extends CI_Controller {
    
        public $username;
        public $status;
        public $user_id;
        public $type = 0;
    
        function index() {
            $this->load->library('session');
            $this->load->helper('url');
            $this->load->helper(array('form', 'url'));
            $this->form_validation->set_rules('username', 'Username', 'required');
            $this->form_validation->set_rules('password', 'Password', 'required');
            $this->form_validation->set_rules('username', 'Username', 'required|callback_check_details');
    
            $this->load->library('form_validation');
    
            if ($this->session->userdata('loggedin') == 1) {//already done
                redirect('/Exams');
            } else if ($this->form_validation->run() == FALSE) {
                $this->load->view('Login_form');
            } else {
    
                if (isset($_REQUEST['redirect']) && $_REQUEST['redirect'] != "") {//anyone wants to get back            
                    redirect($_REQUEST['redirect']);
                } else {
                    redirect('/Exams'); //otherwise
                }
            }
        }
    
        function check_details() {
            $this->load->library('session');
            $username = $this->db->escape($this->input->post('username'));
            $this->username = $username;
            $password = $this->input->post('password');
    
            $query = $this->db->query("select *  from users where username=$username or email=$username");
            if ($query->num_rows() > 0) {
                $row = $query->row();
                if ($row->active == 0 || $row->active == 2) {
                    $this->form_validation->set_message('check_details', 'Your account has not activated yet. Please Check your email');
                    return FALSE;
                }
                if ($this->bcrypt->check_password($password, $row->password)) {
    
                    $this->user_id = $row->user_id;
                    $this->type = $row->type;
                    $this->status = 1;
                    $this->session->set_userdata('loggedin', 1);
                    $this->session->set_userdata('username', $this->username);
                    $this->session->set_userdata('user_id', $this->user_id);
                    $this->session->set_userdata('type', $this->type);
                    $this->session->set_userdata('full_name', $row->full_name);
                    $this->session->set_userdata('roll_number', $row->roll_number);
                    $this->session->set_userdata('phone_number', $row->phone_number);
                    $this->session->set_userdata('profile_picture', $row->profile_picture);
                    $this->session->set_userdata('level', $this->permissions->get_level());
    
                    $this->logger->insert('Logged in.', TRUE, TRUE);
                    return TRUE;
                }
            }
            $this->form_validation->set_message('check_details', 'The username or password is incorrect ');
            return FALSE;
        }
    
    }
    

    【讨论】:

    • 会话库已包含在 autoload.php 中,并且还尝试了您的代码,但仍然是同样的问题 @forge-web-design
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 2018-09-22
    相关资源
    最近更新 更多