codeigniter中的图像验证码,如果用户不理解验证码,则更改为下一验证码。如何创建它?

时间:2015-10-16 04:44:20

标签: image codeigniter codeigniter-2 captcha simplecaptcha

如果用户不理解,如何更改当前验证码图像?

如何在codeigniter中创建,验证和保存数据库中的验证码?

1 个答案:

答案 0 :(得分:0)

您可以使用Ajax进行Captcha刷新。在刷新按钮上调用Js功能按钮

Javascript代码

<script type="text/javascript">
    function RefreshSecurityCode(){
        $.getJSON( "<?php echo base_url(); ?>orders/getCaptchImage", function( data ) {}).success(function(data){
            $("#captcha_image").html(data.captcha);
        });
        return !1;
    }
</script>

控制器功能

 function getCaptchImage(){
        $response['captcha'] = $this->generateCaptcha();

        echo json_encode($response);
        exit;
    }



 function generateCaptcha() {

    //Load Captcha helper
    $this->load->helper('captcha');

        $vals = array(
            'word'       => 'Security Key words',
            'img_path'   => './uploads/captcha/',
            'img_url'    => base_url() . 'captcha/',
            'img_width'  => 200,
            'img_height' => 50,
            'expiration' => 7200,
        );

        /* Generate the captcha */
        $captcha = create_captcha($vals);

        /* Store the captcha value (or 'word') in a session to retrieve later */
        $this->session->set_userdata('captchaWord', $captcha['word']);
        $this->session->set_userdata('captchaImage', $captcha['image']);

        return $captcha['image'];
    }

浏览

<form role="form" method="post" action="<?php echo base_url(); ?>submit">
    <div class="form-group">
        <label>Security Code</label><br/>
        <span id="captcha_image"><?php echo $captcha; ?></span>
    </div>
    <div class="form-group">
        <input type="text" name="captcha" id="captcha" class="form-control" placeholder="Please enter code above" required/>
    </div>
    <div class="form-group">
        <button class="btn" data-loading-text="Loading..." onclick="RefreshSecurityCode()"><i class="fa fa-refresh fa-lg"></i> </button>
        <button class="btn btn-danger btn-bg submit" type="submit" data-loading-text="Sending..."><i class="fa fa-envelope fa-lg"></i> Send Message</button>
    </div>
</form>