验证码验证不匹配

时间:2018-07-12 05:40:28

标签: php codeigniter

我已经在我的表单中创建了验证码验证。一切正常,但是无法通过下一个屏幕进行验证

$this->form_validation->set_rules('regno', 'regno','required');
    $this->form_validation->set_rules('dob', 'dob','required');

    $this->form_validation->set_rules('captcha', 'Security Code', 'trim|required|checkcaptcha');

    if($this->form_validation->run() === FALSE)
    {
        $this->index();
        echo "test";

    }
    else
    {

仅检查第一个条件。我想搬出其他条件。

public  function checkcaptcha()
{   
    if ($_SESSION["code"] == $this->input->post('captcha')) {
        $json['success']  = true;
    } else {
        $json['error']  = true;
    }
    echo json_encode($json);

}

if($this->form_validation->run() === FALSE && ($_SESSION["code"] == $this->input->post('captcha')))

我做到了,现在可以了。正确

1 个答案:

答案 0 :(得分:0)

希望这对您有帮助:

应该是这样的:

public  function checkcaptcha($str)
{   
    if ($_SESSION["code"] == $str ) 
    {
        return TRUE;
    } else {
        $this->form_validation->set_message('checkcaptcha', 'The {field} is invalid');
        return FALSE;
    }
}

别忘了在表单中使用echo validation_errors()方法来查看错误

请阅读:https://www.codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods