dsflxcfuw27742248 2013-01-15 07:56
浏览 29
已采纳

使用codeigniter的验证码不起作用

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

Here is my code :

//Controller file : 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');
    }

}
?>

//view file : 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>

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

  • 写回答

2条回答 默认 最新

  • duanmengsuo9302 2013-01-15 08:35
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法