使用codeigniter的验证码不起作用

时间:2013-01-15 07:56:49

标签: php codeigniter captcha

我正在尝试使用codeigniter实现验证码功能。但它没有显示验证码图像。

这是我的代码:

//控制器文件:captcha.php

<?php

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

    public function __construct() {
        parent::__construct();
        $this->load->view('header');
    }

    /* Function To load captcha view file */

    function list_captcha() {

// loding captcha helper

        $this->load->helper('captcha');
        $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);
        $this->load->view('footer');
    }

}
?>

//查看文件:captcha.php

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

我没有在这段代码中弄错...

2 个答案:

答案 0 :(得分:3)

我相信你的问题就在这一行:

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

您的网址缺少http://协议标识符。

img_url永远不会被CAPTCHA助手修改 - 它直接用于创建<img>标记,该标记会直接转储到HTML输出中。

当浏览器尝试加载图片时,它没有意识到它是一个网址,因此无法加载。

您应修改此行代码以包含http://部分。更好的是,您应该使用CodeIgniter的base_url()函数生成URL,以便您的代码可以灵活应对未来的更改。

答案 1 :(得分:0)

你可以制作如下的验证码。

$values = array(
            'word' => '',   //Generate alternate word by default. You can also set your word.
            'word_length' => 6,  // To set length of captcha word.
            'img_path' => './images/',   // Create  folder "images" in root directory, and give path.
            'img_url' => base_url() .'images/',  // To store captcha images in "images" folder.
            'font_path' => FCPATH.'system/fonts/texb.ttf',
            'img_width' => '250',   //Set image width.
            'img_height' => 50,   // Set image height.
            );

        // "create_captcha" is function of "captcha helper", this will set array in helper library.
        $captcha = create_captcha($values); 

注意: - font_path应包含绝对路径。