首页 文章

会话空codeigniter

提问于
浏览
2

首先,对不起我的英语:D

我尝试在没有成功的情况下在Codeigniter上进行简单的登录 . 我阅读了官方文档并将脚本减少到最低限度 . 每当我重定向或刷新我的页面或我去另一个控制器时,我的会话都是空的 . 如果我只创建一个控制器并进行简单的读写操作,那么在我刷新或重定向之前它一直很顺利 .

这就是我想要做的事情:1)第一个控制器是主控制器 . 使用登录表单加载视图 . 此表单有一个方法validation_form,然后是username_check回调 .

2)如果用户能够登录,我设置我的userdata并重定向到限制控制器,这是我的禁区 .

附:我在自动加载中使用库会话,并且选项数据库处于活动状态 . 我也有autoload中的数据库库 .

主控制器

class Main extends CI_Controller {


public function __construct(){
    parent::__construct();

}

public function index()
{
    $this->load->view('login_view');
}

public function validation_form()
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules('email', 'Email', 'required|callback_username_check');
    if ($this->form_validation->run() == FALSE){
       echo 'validation_error';
    }else{
       redirect('/restricted');
    }
}

public function username_check(){
    $this->load->model('login_model');
    $email=$this->input->post('email');
    $login=$this->login_model->validate($email);
    if ($login != FALSE){                    
                return true;
    }
    return false;
}

}

登录模型

class Login_model extends CI_Model{

public function __construct(){
    parent::__construct();
}

 public function validate($email){

    // Prep the query
    $this->db->where('EmailDatore', $email);

    // Run the query
    $query = $this->db->get('Datore');
    // Let's check if there are any results
    if($query->num_rows() == 1)
    {
        // If there is a user, then create session data
        $row = $query->row();
        $data = array(
                'email' => $row->EmailDatore,
                'nome' => $row->Nome,
                'cognome' => $row->Cognome,                    
                'validated' => true
                );
        $this->session->set_userdata($data);
        return true;
    }
    // If the previous process did not validate
    // then return false.
    return false;
}

}

RESTRICTED CONTROLLER类Restricted扩展CI_Controller {

public function __construct(){
    parent::__construct();
    $this->check_isvalidated();
}

public function index(){
    // If the user is validated, then this function will run
    echo 'Congratulations, you are logged in.';
}

private function check_isvalidated(){
    if(! $this->session->userdata('validated')){
        redirect('main');
    }
}

}

2 回答

  • 1

    我认为回调函数也可以这样 .

    public function username_check($str){
       $this->load->model('login_model');
       $login=$this->login_model->validate($str);
       if ($login != FALSE){                    
                return true;
       }
    return false;
    

    您的代码是否已达到模型或是否未通过回调?代码的哪些部分未达到?放置vardump();或者死();去检查 .

  • 1

    模型中的错字,不确定是否还有其他错误,但至少有这个错误,这意味着你永远不会完成你的成功代码 .

    if($query->num_rows() == 1)
    

    编辑 - 错误也在这里,你传递$ data但是人口$ email . 因此模型得到一个空字符串 .

    public function username_check(){
    $this->load->model('login_model');
    $email=$this->input->post('email');
    $login=$this->login_model->validate($email);
    if ($login != FALSE){                    
                return true;
    }
    return false;
    }
    

相关问题