我在localhost上安装了codeigniter
main.php控制器是
class Main extends CI_Controller {
public function index()
{
$this -> login();
}
public function login()
{
$this->load->view('login');
}
public function login_validation() {
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('password','Password','required|md5');
if($this->form_validation->run())
{
redirect('main/members');
}
else
{
$this->load->view('login');
}
}
}
登录正在工作页面即将到来,但是在我填写用户名和密码之后,应该带我登录/ main / login_validation,然后函数login_validation()应该重定向到main / members或者显示登录页面但是,当我提交表单时,会发现未找到的对象错误即将发生。任何人都可以帮助我吗?
表单是
form_open( '主/ login_validation');
答案 0 :(得分:2)
要回答我认为您的问题正在发生的事情,您使用的是form_validation类错误。您应该测试form_validation-> run()是否为TRUE(所有字段都通过验证)或FALSE(一个或多个输入中存在错误)。您编码的结构如下:
public function login_validation() {
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('password','Password','required|md5');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('login');
}
else
{
redirect('main/members');
}
}
CodeIgniter文档here中介绍了这一点。
答案 1 :(得分:0)
在您的控制器中..你可以尝试这个..
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected to login page
$this->load->view('login');
}
else
{
$this->check_database();
//redirect('home');
}
}
function check_database()
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
$password = $this->input->post('password');
//query the database
$query_result = $this->model_login->login($username, $password);
if($query_result)
{
$this->load->view('main/members');
}
else
{
redirect('login');
}
}
在模型中
function login($username,$password){
$query = $this->db->query("SELECT * FROM `login` WHERE `username`= '".$username."' AND
password='".$password."' LIMIT 1");
if ($query->num_rows() > 0)
return TRUE;
else
return FALSE;
}