model.php
public function verifyLogin($username, $entered_password, $captcha_input) {
$this->db->trans_start();
$sql = "SELECT
name,
selector,
password,
login_attempts
FROM
company
WHERE
username = ?
LIMIT
1
";
$query = $this->db->query($sql, $username);
$result_num = $query->num_rows();
if ($result_num == 1) {
$stored_password = $query->row()->password;
$login_attempts = $query->row()->login_attempts;
if (crypt($entered_password, $stored_password) == $stored_password) {
if ($captcha_input == $this->session->userdata('captchaWord'))
{
//recreate captcha
}
$data['company_name'] = $query->row()->name;
$data['company_selector'] = $query->row()->selector;
//Not working
$sql = "UPDATE
company
SET
login_attempts = 0
WHERE
username = ?
";
$query = $this->db->query($sql, $username);
unset($this->captcha);
return $data;
}
else {
if ($login_attempts >= 3)
{
//Enable CAPTCHA
$random_number = substr(number_format(time() * rand(),0,'',''),0,6);
$vals = array(
'word' => $random_number,
'img_path' => './captcha/',
'img_url' => base_url().'captcha/',
'font_path' => './path/to/fonts/texb.ttf',
'img_width' => '150',
'img_height' => 30,
'expiration' => 7200,
'word_length' => 8,
'font_size' => 16,
'img_id' => 'Imageid',
'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
// White background and border, black text and red grid
'colors' => array(
'background' => array(255, 255, 255),
'border' => array(255, 255, 255),
'text' => array(0, 0, 0),
'grid' => array(255, 40, 40)
)
);
$this->captcha = create_captcha($vals);
$this->session->set_userdata('captchaWord', $random_number);
}
//Not working
$sql = "UPDATE
company
SET
login_attempts = login_attempts + 1
WHERE
username = ?
";
$query = $this->db->query($sql, $username);
echo $this->db->last_query();
return FALSE;
}
} else {
return 0;
}
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE) {
return 'Transaction failed';
}
}
Controller.php这样
public function testAdminLogin() {
$this->load->model('Company_model');
$is_logged_in = $this->Company_model->verifyLogin('sarah', '33123', '');
if ($is_logged_in === 0) {
echo "No username found";
}
else if ($is_logged_in === FALSE) {
echo "Password not matched";
$data['cap'] = $this->Company_model->getCaptcha();
if (isset($data['cap']))
{
$this->load->view('test_captcha', $data);
}
}
else if ($is_logged_in === 'Transaction failed') {
//possibly an error
}
else {
//successful
$session_array = array(
'company_selector' => $is_logged_in['company_selector'],
'company_name' => $is_logged_in['company_name']
);
$this->session->set_userdata('session_array', $session_array);
echo "Login successful. Company selector: ".$this->session->userdata['session_array']['company_selector'];
}
}
当我运行它时,数据库中的记录不会更新。它应该增加1但值保持不变。
从$this->db->last_query()
回显的SQL语句是正确的。我将其直接复制并粘贴到MySQL工作台中,更改生效。
我尝试使用Active Records也无济于事。
答案 0 :(得分:0)
它与$this->db->trans_complete();
有关。当用户输入错误的密码时,它会立即返回一个值,因此事务永远不会完成,并且永远不会提交更改。
不得不设置标志来检查密码是否错误。
private $wrong_password = FALSE;
if ($login_attempts >= 3)
{
//Enable CAPTCHA
$random_number = substr(number_format(time() * rand(),0,'',''),0,6);
$vals = array(
'word' => $random_number,
'img_path' => './captcha/',
'img_url' => base_url().'captcha/',
'font_path' => './path/to/fonts/texb.ttf',
'img_width' => '150',
'img_height' => 30,
'expiration' => 7200,
'word_length' => 8,
'font_size' => 16,
'img_id' => 'Imageid',
'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
// White background and border, black text and red grid
'colors' => array(
'background' => array(255, 255, 255),
'border' => array(255, 255, 255),
'text' => array(0, 0, 0),
'grid' => array(255, 40, 40)
)
);
$this->captcha = create_captcha($vals);
$this->session->set_userdata('captchaWord', $random_number);
}
// $data = array(
// 'login_attempts' => 'login_attempts + 1'
// );
// $this->db->where('username', $username);
// $this->db->update('company', $data);
$sql = "UPDATE
company
SET
login_attempts = login_attempts + 1
WHERE
username = ?
";
$query = $this->db->query($sql, $username);
$this->wrong_password = TRUE;
然后在$this->db->trans_complete();
之后:
if ($this->db->trans_status() === FALSE) {
return 'Transaction failed';
} else {
if ($this->wrong_password === TRUE) {
return FALSE;
}
}
这是一个诚实的错误。