我尝试使用此教程http://www.iluv2code.com/login-with-codeigniter-php.html编写登录名,但我对veralidate密码有问题。我的数据库看起来相同,当我一直尝试登录时,我都有 'Invalid username or password'
但是,如果我在 VerifyLogin.php
中做if(1)
而不是if($result)
,则可以在验证中查询问题。有人知道如何改善吗?还是如何从此数据库中获得密码?我尝试比较手动...
模型:
function __construct() {
parent::__construct();
$this->load->database();
}
function login($username, $password)
{
$this -> db -> select('id, username, password');
$this -> db -> from('users');
$this -> db -> where('username', $username);
$this -> db -> where('password', MD5($password));
$this -> db -> limit(1);
$query = $this -> db -> get();
if($query -> num_rows() == 1)
{
return $query->result(); //row
}
else
{
return false;
}
}
}
?>
使用isset
if(isset($query))
{
return $query->result(); //row
}
$username = $this->input->post('username');
$password = $this->input->post('password');
$result = $this->users->checkloginauth($username, $password);
if(isset($result) && count($result) > 0)
{
$this->session->set_userdata('admin_id', $result[0]['id']);
$this->session->set_userdata('firstname', $result[0]['firstname']);
$this->session->set_userdata('lastname', $result[0]['lastname']);
$this->session->set_userdata('email', $result[0]['email']);
$this->session->set_userdata('status', '1');
redirect(site_url('home'), 'refresh');
exit;
}
else
{
$data['errormsg'] = "Invalid UserName or password.";
$this->load->view('login',$data);
}
如果您使用MD5,则写$ password = md5($ this-> input-> post('password'));
在用户模型中
public function checkloginauth($username, $password)
{
$this->db->select();
$this->db->from($this->users." AS u");
$this->db->where('u.username', $username);
$this->db->where('u.password', $password);
$result = $this->db->get();
return $result->result_array();
}
result_array()返回数组中的值,以便数据存储在$ [0]
这不能允许使用USENAME和密码不应该像chrodres和数字。
public function checkloginauth($username, $password)
{
$this->db->select();
$this->db->from($this->users." AS u");
$this->db->where('u.username LIKE BINARY', $username);
$this->db->where('u.password LIKE BINARY', $password);
$result = $this->db->get();
return $result->result_array();
}