无法使用codeIgniter修复错误

时间:2014-08-01 06:21:41

标签: php codeigniter

我刚刚使用codeIgniter,我想用注册和会话创建一个简单的登录。

这是我的代码......

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

class LoginVerify extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('userModel', '', TRUE);
    }

    function index() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Username', 'trim|required');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|callback_this_database');
        if($this->form_validation->run()==FALSE) {
            $this->load->view('login_view');
        }
        else {
            redirect('home', 'refresh');
        }
    }

    function this_database($password) {
        $username=$this->input->post('username');
        $result=$this->user->login($username, $password);
        if($result) {
            $sess_array=array();
            foreach($result as $row) {
                $sess_array=array('id'=>$row->id, 'username'=>$row->username);
                $this->session->set_userdata(logged_in, $sess_array);   
            }
            return TRUE;
        }
        else {
            $this->form_validation->set_message(this_database, 'Invalid username or password');
            return FALSE;
        }
    }
}
?>

我不知道为什么我在尝试运行此程序时仍然收到错误。

4 个答案:

答案 0 :(得分:2)

我认为因为这个

    else {
        redirect('home', 'refresh');
        //means you are going to controller named home, but your controller name is LoginVerify
    }

猜它会解决你的问题

答案 1 :(得分:1)

您在回调时调用数据库查询尝试验证成功后的方法部分也为usermodel创建别名。还检查您是否已包含会话库。 你还留下了语法错误,所以试试我的完整代码: -

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

class LoginVerify extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('userModel', 'user');
    }

    function index() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Username', 'trim|required');
        $this->form_validation->set_rules('password', 'Password', 'trim|required');
        if($this->form_validation->run()==FALSE) {
            $this->load->view('login_view');
        }
        else {
            $username=$this->input->post('username');
            $password=$this->input->post('password');
            $result=$this->user->login($username, $password);
            if($result) {
                $sess_array=array();
                foreach($result as $row) {
                    $sess_array=array('id'=>$row->id, 'username'=>$row->username);
                    $this->session->set_userdata('logged_in', $sess_array);   
                }
                redirect('home', 'refresh');
            }
            else {
                $this->form_validation->set_message('this_database', 'Invalid username or password');
                redirect('home', 'refresh');
            }

        }
    }
}
?>

答案 2 :(得分:1)

$this->form_validation->set_rules('username', 'Username', 'trim|required');

$this->form_validation->set_rules('password', 'Password', 'trim|required');

答案 3 :(得分:-2)

尝试改变

  

$ this-&gt; load-&gt; model('userModel','',TRUE);

  

$ this-&gt; load-&gt; model('userModel','user');

并最好删除脚本末尾的php close标记

相关问题