Captcha using codeigniter does not work


I am trying to implement captcha functionality using codeigniter. But it is not displaying captcha images.

Here is my code :

//Controller file : captcha.php


class Captcha extends CI_Controller {
    var $controller = "captcha";
    var $viewContent = array();

    public function __construct() {

    /* Function To load captcha view file */

    function list_captcha() {

// loding captcha helper

        $data = array(
            'img_path' => './captcha/',
            'img_url' => 'localhost/demoProject/application/captcha/',
            'img_width' => '150',
            'img_height' => 24,
            'border' => 0,
            'expiration' => 7200
        $captchaArr = create_captcha($data);
        $this->viewContent['captchaArr'] = $captchaArr;
        $this->viewContent['captchaWord'] = $captchaArr['word'];
        //echo"<pre>"; print_r($captchaArr);exit;
        $this->viewContent['refreshUrl'] = "localhost/
        demoProject/" . $this->controller . "/list_" . $this->controller;
        $this->load->view($this->controller, $this->viewContent);


//view file : captcha.php

            if($("#captcha").val()== '<?php echo $captchaWord; ?>'){
                alert('Captcha match');
                alert('Please enter same word like captcha image.');
            Captcha Image
        <td style="text-align: center">
            &nbsp;&nbsp;<?php echo $captchaArr['image']; ?><br>
        <td>Enter the same code here </td>
        <td style="text-align: center">&nbsp;&nbsp;
            $data = array('name' => 'captcha', 'id' => 'captcha');
            echo form_input($data);
        <td>Can't read the image?</td>
        <td style="text-align: center">&nbsp;&nbsp;<a href="<?php echo $refreshUrl; ?>">Refresh</a></td>

I am not getting what is wrong in this code...

I believe your problem is this line:

img_url' => 'localhost/demoProject/application/captcha/',

Your URL is missing the http:// protocol identifier.

img_url is never modified by the CAPTCHA helper - it is directly used to create the <img> tag, which is dumped right into your HTML output.

When the browser attempts to load the image, it doesn't realize that it's a web address, so it fails to load.

You should modify this line of code to include the http:// portion. Better yet, you should be generating the URL using CodeIgniter's base_url() function, so that your code will be flexible to future changes.