A CAPTCHA is a randomly generated string (or photo group) that appears when verification is required. It is an essential requirement for cutting spam on a website.


Project structure

Before to make a captcha in Codeigniter first see the file structure of the project 




Create Controller

Create a file named application/controllers/Captcha.php in the Controller folder. Open the file in your code editor and add the following code to it:


application/controllers/Captcha.php

<?php

defined('BASEPATH') OR exit('your exit message');

class Captcha extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('captcha');
    }

    public function index() {
        if ($this->input->post('submit')) {
            $captcha_insert = $this->input->post('captcha');
            $contain_sess_captcha = $this->session->userdata('valuecaptchaCode');
            if ($captcha_insert === $contain_sess_captcha) {
                echo 'Success';
            } else {
                echo 'Failure';
            }
        }
        $config = array(
            'img_url' => base_url() . 'image_for_captcha/',
            'img_path' => 'image_for_captcha/',
            'img_height' => 45,
            'word_length' => 5,
            'img_width' => '145',
            'font_size' => 20
        );
        $captcha = create_captcha($config);
        $this->session->unset_userdata('valuecaptchaCode');
        $this->session->set_userdata('valuecaptchaCode', $captcha['word']);
        $data['captchaImg'] = $captcha['image'];
        $this->load->view('mycaptcha', $data);
    }

    public function refresh() {
        $config = array(
            'img_url' => base_url() . 'image_for_captcha/',
            'img_path' => 'image_for_captcha/',
            'img_height' => 45,
            'word_length' => 5,
            'img_width' => '145',
            'font_size' => 20
        );
        $captcha = create_captcha($config);
        $this->session->unset_userdata('valuecaptchaCode');
        $this->session->set_userdata('valuecaptchaCode', $captcha['word']);
        echo $captcha['image'];
    }

}

Create Views

create a view to show the captcha image and input it to enter captcha code for verifying it


application/views/mycaptcha.php

<!DOCTYPE html>
<html>
    <head>
        <title>Implement Captcha in Codeigniter using helper</title>
        <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>

        <script>
            $(document).ready(function() {
                $('.captcha-refresh').on('click', function() {
                    $.get('<?php echo base_url() . 'Captcha/refresh'; ?>', function(data) {
                        $('#image_captcha').html(data);
                    });
                });
            });
        </script>

    </head>

    <body>

        <p id="image_captcha"><?php echo $captchaImg; ?></p>

        <a href="javascript:void(0);" class="captcha-refresh" ><img style="width: 20px;"src="<?php echo base_url() . 'images/refresh.jpg'; ?>"/></a>
        <form method="post">
            <input type="text" name="captcha" value=""/>
            <input type="submit" name="submit" value="SUBMIT"/>
        </form>
    </body>
</html>

Output:




Download source code

Are you facing problems in understanding this article? download source code now