可能很简单,但这是我的问题,我已经在我的用户系统中实现了一个简单的验证码,但我似乎无法使用回调表单验证来检查验证码是否正确。
我将答案存储在用户会话中,并在表单验证时检查它是否正确。
你可以在这里看到它:http://77.96.119.180/beer/user/register
请原谅对啤酒的任何引用;)
这是我的代码:
$this->form_validation->set_rules('captcha', 'Image verification', 'trim|required|xss_clean|integer|callback_captcha_check');
这是表格的一部分
// CAPTCHA
$random_word = rand(10000, 99999);
$vals = array(
'word' => $random_word,
'img_path' => './captcha/',
'img_url' => 'http://77.96.119.180/beer/captcha/',
'font_path' => './captcha/fonts/pdark.ttf',
'img_width' => '150',
'img_height' => 50,
'expiration' => 7200
);
$cap = create_captcha($vals);
// Store captcha answer in session
$this->session->set_userdata('captcha_answer', $random_word);
$content .= form_label('Please enter the numbers in the box to verify you are human (Hint: Only numbers will appear)', 'captcha');
$content .= '<div class="left" style="margin-left:20px;">' . $cap['image'] . '</div>';
$content .= form_input(array('name' => 'captcha', 'value' => '', 'class' => 'right', 'style' => 'width:75%; margin-top:5px;'));
这是回调函数
public function captcha_check($str)
{
if ($str == $this->session->userdata('captcha_answer'))
{
// Captcha is correct
$this->form_validation->set_message('captcha', 'The %s was wrong, please try again');
return FALSE;
}
else
{
return TRUE;
}
}