我的控制器。
//Load Login Page------------------------------------------------------------------------------------------------------------------------------------------------------------
function login(){
$this->load->helper('form');
$data['title'] = "Login";
$this->load->view("view_header",$data);
$this->load->view("view_login", $data);
}
//Login Validation------------------------------------------------------------------------------------------------------------------------------------------------------------
public function login_validation(){
$this->load->library('form_validation');
$this->form_validation->set_rules('Username', 'Username', 'required|trim');
$this->form_validation->set_rules('Password', 'Password', 'required|trim|md5');
if ($this->form_validation->run()){
redirect('site/members');
}
else{
$this->load->view('view_header');
$this->load->view('view_login');
}
}
public function validate_credentials(){
$this->load->model('model_users');
if ($this->model_users->canLogin()){
return true;
}
else{
$this->form_validation->set_message('validate_credentials', 'Incorrect username or password');
return false;
}
}
//Load Members Page------------------------------------------------------------------------------------------------------------------------------------------------------------
public function members(){
$data['title'] = "Members";
$this->load->view('view_header');
$this->load->view("view_members", $data);
}
我的模特
class Model_users extends CI_Model {
public function canLogin(){
$this->db->where('Username', $this->input->post('Username'));
$this->db->where('Password', md5($this->input->post('Username')));
$query = $this->db->get('user_registration');
if ($query->num_rows() == 1){
return true;
}
else{
return false;
}
}
我的表格
<?php
echo form_open('site/login_validation');
echo form_label('Username:', 'Username');
echo form_input('Username');
echo "<br>";
echo form_label('Password:', 'Password');
echo form_password('Password');
echo "<br>";
echo form_submit('Login', 'Login');
echo validation_errors();
echo form_close();
?>
似乎无法获取我的用户名和密码进行验证。它允许我在没有现有帐户的情况下访问成员页面。因为我一直在关注Youtube教程,所以无法找到问题。
答案 0 :(得分:0)
尝试此代码并使用回调函数进行表单验证,您可能需要在routes.php $ route [&#39; login&#39;] =&#34; login / index&#34 ;;
请勿使用 MD5 仅将其用于测试。
class Login extends CI_Controller {
public function index() {
$this->load->library('form_validation');
$this->load->helper('form');
$this->load->helper('url');
$data['title'] = "Login";
$this->form_validation->set_rules('Username', 'Username', 'required|trim|callback_validate_credentials');
$this->form_validation->set_rules('Password', 'Password', 'required|trim|md5');
if ($this->form_validation->run() == FALSE ){
$this->load->view('view_header');
$this->load->view('view_login');
} else{
redirect('site/members');
}
}
public function validate_credentials(){
$this->load->library('form_validation');
$this->load->model('model_users');
if ($this->model_users->canLogin()){
return true;
} else{
$this->form_validation->set_message('validate_credentials', 'Incorrect username or password');
return false;
}
}
}
查看
<?php
echo form_open('login');
echo form_label('Username:', 'Username');
echo form_input('username'); // Lower case
echo "<br>";
echo form_label('Password:', 'Password');
echo form_password('password'); // Lower case
echo "<br>";
echo form_submit('Login', 'Login');
echo validation_errors();
echo form_close();
?>