我在使用验证码进行表单验证方面存在问题,我在会话中存储的表单总是在每次提交表单后获取新代码,并且post变量输入的最后一个代码从不匹配。如何在比较回调函数中的代码之前取消设置会话。 (下面的captcha_code_check)
function captcha_code_check($code)
{
$row_count = $this->captcha_model->check_captcha($code);
if ($code == $this->session->userdata('word') && $row_count == 0) {
$this->form_validation->set_message('captcha_code_check', 'Security Code is invalid.');
return FALSE;
}
else {
return TRUE;
}
} function _init_captcha() {
$this->load->helper('captcha');
$this->load->model('captcha_model');
$this->load->library('session');
$vals = array(
'img_path' => './_resources/captcha/',
'img_url' => base_url().'_resources/captcha/',
'img_width' => '134',
'img_height' => 30,
'border' => 0,
'expiration' => 3600 // one hour
);
$captcha = create_captcha($vals);
$capdb = array(
'captcha_id' => '',
'captcha_time' => $captcha['time'],
'ip_address' => $this->input->ip_address(),
'word' => $captcha['word']
);
$this->captcha_model->insert_captcha($capdb);
// store the captcha word in a session
$this->session->set_userdata('word', $captcha['word']);
return $captcha;
} the model returns row 1 as the correct code entered which is compared is already present in db. I tried to put session code in compariosion, but the session code shows always different than the code entered. how to unset the session code and at which point unset is to be made and comparision should through error. Please advise how to resolve. thanks.