我有一个登录方法,但它似乎没有正常工作。例如,如果我尝试使用数据库中存在的用户名和另一个用户的密码登录,我可以成功登录。有人可以在我的代码中发现错误吗?
控制器:
public function login(){
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('', '');
$this->form_validation->set_rules('name_login', 'Utilizator', 'required|callback_validate_login|xss_clean|trim');
$this->form_validation->set_rules('password_login', 'Password', 'required|xss_clean|trim');
$this->form_validation->set_message('validate_login','login esuat');
$this->form_validation->set_message('required', '*');
if ($this->form_validation->run() === FALSE){
$this->render('login');
}
else {
// user successful login
$data = array(
'username' => $this->input->post('name_login'),
'is_logged_in' => 1
);
$this->session->set_userdata($data);
$this->load->view('homeview',$data);
}
}
public function validate_login(){
$username = $this->input->post('user_login');
$pass = $this->input->post('password_login');
if ($this->homemodel->login($username,$pass)){
return true;// form validation passed, u are now logged in
}
else {
return false;
}
}
型号:
public function login($user, $pass)
{
$query = $this->db->get_where('login', array('username' => $user, 'password' => $pass));
echo '<pre>';
print_r($query->result());
echo '</pre>';
if ($query->num_rows() === 1) {
return true;
} else {
return false;
}
查看
<?php echo form_open('home/login') ?>
<label for="user">Username</label>
<input type="text" class="createform" name="user_login" value="<?php echo set_value('name_login'); ?> " />
<?php echo form_error('name_login') . '<br>'; ?>
<label for="pass">Password</label>
<input type="text" class="createform" name="password_login" value="<?php echo set_value('password_login'); ?> " />
<?php echo form_error('password_login') . '<br>'; ?>
<input type="submit" name="submit" value="Login" />
</form>
答案 0 :(得分:1)
哦,我的不好,发现了错误:视图文件中的用户名输入名称为&#34; user_login&#34;在控制器中,我写了'name_login&#39;:
$这 - &GT; form_validation-&GT; set_rules(&#39; name_login&#39;,...)
答案 1 :(得分:0)
看起来您只是在成功提交表单时将会话数据设置为is_logged_in
:
if ($this->form_validation->run() === FALSE){
$this->render('login');
}
else {
// user successful login
$data = array(
'username' => $this->input->post('name_login'),
'is_logged_in' => 1
);
您需要先验证用户。
我会尝试类似的事情:
if ($this->form_validation->run() === FALSE){
$this->render('login');
}
else {
$results = $this->validate_login();
// user successful login
if ($results){
$data = array(
'username' => $this->input->post('name_login'),
'is_logged_in' => 1
);
$this->session->set_userdata($data);
$this->load->view('homeview',$data);
}
else {
redirect('login');
}
}
这有意义吗?看起来您实际上并没有检查或调用validate_login()
方法。