正确的用户名和密码后,页面不会重定向

时间:2015-08-06 08:32:27

标签: php mysql codeigniter

<body>
<?php echo validation_errors(); ?>
<?php echo form_open('LoginController/checkLogin'); ?>

Username:
<input type="text" name="username" /> </br> </br>
Password :
<input type="text" name="password" /> </br> </br>
<input type="submit" name="submit" value="Login"/>
</form>
</body>

class LoginController extends CI_Controller
{
	public function index()
	{
		$this->load->view('login');
	}
	public function checkLogin()
	{
		$this->form_validation->set_rules('username','Username','required|valid_email');
		$this->form_validation->set_rules('password','Password','required|callback_verifyUser');
		
		if($this->form_validation->run()== false)
		{
			$this->load->view('login');
		}
		else
		{
			redirect('HomeController/index');
			//$this->load->view('home');
			
		}
	}
	public function verifyUser()
	{
		$user=$this->input->post['username'];
		$pass=$this->input->post['password'];
		
		$this->load->model('LoginModel');
		if($this-> LoginModel ->login($user, $pass)){
			return true;
		}
		else
		{
			$this->form_validation->set_message('verifyUser','Incorrect user........');
			return false;
		}
	}
}

正确的用户名和密码页面未重定向到主页。我做错了请帮助我。您的帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

加载

$this->load->helper('url');

redirect('HomeController/index');更改为redirect(base_url() . 'HomeController');

  

注意:如果您重定向到控制器index(),默认情况下会调用该函数

编辑01

public function checkLogin()
{
    $this->form_validation->set_rules('username','Username','required|valid_email');
    $this->form_validation->set_rules('password','Password','required|callback_verifyUser');

    if($this->form_validation->run()== false)
    {
        $this->load->view('login');
    }
    else
    {
        $user= $_POST['username'];
        $pass= $_POST['password'];

        $this->load->model('LoginModel');
        $result = $this->LoginModel->login($user, $pass);
        if($result==1)
        {
            //call your page
            //redirect(base_url() . 'HomeController');
        }
        else
        {
            //$this->form_validation->set_message('verifyUser','Incorrect user........');
            $this->load->view('login');//form view
        }

    }

}

在模型中

public function login($user, $pass)
{
    $query = $this->db->query("SELECT *FROM members WHERE user='$user' AND pass='$pass'");
    $result = $query->result_array();

    $count = count($result);
    if($count==1)
    {
        $log = 1;
        return $log;
    }
    else
    {
        $log = 0;
        return $log;
    }
}