我在注册表单时有以下代码:
function register()
{
$this->form_validation->set_rules('txt_username', 'Username', 'trim|required');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required|matches[txt_password]|md5');
$this->form_validation->set_rules('txt_password', 'Password', 'trim|required|md5');
//other codes
$data = array('username' => $this->input->post('txt_username'),
'password' => $this->input->post('txt_password')
);
// insert form data into database
if ($this->account_model->insertUser($data));
{
$this->session->set_flashdata('msg','successfully registered!');
redirect('account/register');
}
}
密码与此处匹配。 但是当我使用相同的数据用户名和密码登录时:它显示无效的用户名或密码: 登录功能如下:
function login()
{
//set validations
$this->form_validation->set_rules("txt_username", "Username", "trim|required");
$this->form_validation->set_rules("txt_password", "Password", "trim|required");
$username = $this->input->post("txt_username");
$password = $this->input->post("txt_password");
$usr_result = $this->account_model->get_user($username, $password);
if ($usr_result > 0) //active user record is present
{
$this->account_model->login();
$data['message'] ="You are logged in!";
}
else
{
$this->session->set_flashdata('msg', 'Invalid username and password!');
}
}
}
我有一个用于将注册表数据插入数据库的模型:
function insertUser($data)
{
return $this->db->insert('user', $data);
}
我有这个模型来检索数据以登录:
function get_user($usr, $pwd)
{
$sql = "select * from user where username = '" . $usr . "' and password = '".md5($pwd). "'";
$query = $this->db->query($sql);
return $query->num_rows();
}
最佳答案
最好的方法instead of MD5
是在Codeigniter中使用base64
。这意味着decode()
和 encode()
.
编码
$password = 'mYp@ssw0rd'; // or $password
$encriptKey = 'super-secret-key';
$encrypted_string = $this->encrypt->encode($password, $encriptKey);
解码
$password = 'mYp@ssw0rd'; // or $password
$encriptKey = 'super-secret-key';
$decrypted_string = $this->encrypt->decode($password, $encriptKey);
此外
同时加载库$this->load->library('encrypt');
关于php - 登录时密码不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34713988/