代码点火器表单验证回调不起作用


Codeigniter form validation callbacks not working

可能很简单,但这是我的问题,我已经将一个简单的验证码植入我的用户系统,但我似乎无法使用回调表单验证来检查验证码是否正确。

我将答案存储在用户会话中,并在表单验证时检查它是否正确。

你可以在这里看到它直播: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;
        }
    }
验证

规则在回调函数中被颠倒。 你的返回 true 和返回 false 逻辑需要颠倒。

只需将您的 if 语句更改为以下内容:

if ($str != $this->session->userdata('captcha_answer'))