How to add Captcha in CodeIgniter form?


In this article, We will discuss add Captcha in CodeIgniter form. We are going to use Codeigniter captcha helper and contains functions that will help in generating CAPTCHA images. Yo needs GD library as helper installed on your server.

First Step, to use a Captcha helper in the CI 3 application:

Download and Install
Step 2: Download CodeIgniter from the official website

Create Controller
Step 3: Now, create a controller with name captcha.php with a given code in application/controllers/captcha.php.

        $this->load->helper(array('form', 'url', 'captcha'));

    /* The default function that gets called when visiting the page */
    public function index()
        /* Set form validation rules */
        $this->form_validation->set_rules('name', "Name", 'required');
        $this->form_validation->set_rules('captcha', "Captcha", 'required');

        /* Get the user's entered captcha value from the form */
        $userCaptcha = $this->input->post('captcha');

        /* Check if form (and captcha) passed validation*/
        if ($this->form_validation->run() == true && strcmp(strtolower($userCaptcha), strtolower($this->session->userdata('captchaWord'))) == 0) {
            /** Validation was successful; show the Success view **/
            /* Clear the session variable */
            /* Get the user's name from the form */
            $name = $this->input->post('name');
            /* Pass in the user input to the success view for display */
            $data = array('name' => $name);
            // do as your requirement
        } else {
            /** Validation was not successful - Generate a captcha **/
            /* Setup vals to pass into the create_captcha function */
            $captcha = $this->_generateCaptcha();
            /* Store the captcha value (or 'word') in a session to retrieve later */
            $this->session->set_userdata('captchaWord', $captcha['word']);
            /* Load the captcha view containing the form (located under the 'views' folder) */
            $this->load->view('captcha_view', $captcha);

    // this function will create captcha
    private function _generateCaptcha()
        $vals = array(
            'img_path' => './captcha_images/',
            'img_url' => base_url('captcha_images/'),
            'img_width' => '150',
            'img_height' => 30,
            'expiration' => 7200,
        /* Generate the captcha */
        return create_captcha($vals);
/* End of file captcha.php */
/* Location: ./application/controllers/captcha.php */

The _generateCaptcha() function will create captcha image and it will add generated image in captcha_images folder as configured and you should generate captcha_images folder in your CI 3 project root folder and given its permission to 777 or 666.

Create a new form

Step 3: Create a View file name with captcha_view.php in the applications/views/captcha_view.php

Also read: How to download Image from the URL in PHP

Like it? Share with your friends!