duandongji2231 2014-03-23 10:26
浏览 54
已采纳

我的codeigniter验证码不生成图像

My CodeIgniter captcha is not generating so I can't show it in my view

Controller

public function login() {
    $this->load->helper('captcha');
    $this->form_validation->set_rules($this->login_rules);
    $userCaptcha = set_value('captcha');
    $word = $this->session->userdata('captchaWord');

    if($this->form_validation->run() == TRUE && strcmp(strtoupper($userCaptcha),strtoupper($word)) == 0) {
        $this->session->unset_userdata('captchaWord');
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        if(!$this->autentifikasi->login($username,$password)) {
            $this->session->set_flashdata('pesan', '<div class="error">Login gagal</div>');
            redirect(site_url('user/login'));
        } else {
            $this->session->set_flashdata('pesan', '<div class="sukses">Anda telah berhasil login, selamat berbelanja.</div>');
            redirect(site_url('user/login'));
        }
    }

    $original_string = array_merge(range(0,9), range('a','z'), range('A', 'Z'));
    $original_string = implode("", $original_string);
    $captcha = substr(str_shuffle($original_string), 0, 6);

    $vals = array(
        'word' => $captcha,
        'img_path' => './captcha/',
        'img_url' => base_url().'user/login/',
        'font_path' => './fonts/texb.ttf',
        'img_width' => '150',
        'img_height' => 30,
        'expiration' => 3600
    );

    $cap = create_captcha($vals);

    $data['image']= $cap['image'];
    $this->session->set_userdata('captchaWord', $captcha);

    $this->template->set_judul('PT. Columbindo Perdana')
        ->set_css('style')
        ->set_parsial('sidebar','sidebar_view',$this->data)
        ->set_parsial('topmenu','top_view',$this->data)
        ->render('user_login',$this->data); 
}

View

    <?php if($this->session->flashdata('pesan')): ?>
    <?php echo $this->session->flashdata('pesan'); ?>
<?php else:?>
    <h2>Silakan Login</h2>
    <?php if(@$error){echo @$error;} ?>
    <?php echo validation_errors(); ?>
    <br />
    <?php echo form_open(site_url(uri_string()),'class="order"'); ?>
    <?php echo form_label('Username'); ?>
    <?php echo form_input('username'); ?>
    <?php echo form_label('Password'); ?>
    <?php echo form_password('password'); ?>
    <?php echo form_label('Captcha'); ?>
    <?php echo $image; ?>
    <?php echo form_input('captcha'); ?>
    <?php echo form_submit('submit','Login'); ?>
    <?php echo form_close(); ?>
    <br />
    Anda belum memiliki akun? Silakan daftar <a href="<?php echo site_url('user/register') ;?>">disini</a>.
<?php endif ?>

As I know I didn't have captcha folder or font folder, is it still will be generate it self without those 2 folders? I already try what i can from some solved problem but it's still not working. no error but the captcha image not show up.

please help me.

  • 写回答

1条回答 默认 最新

  • doson1998 2014-03-23 11:30
    关注

    The problem is that you are passing $this->data to your view but you are assigning the image generated to $data. So you need to change it from

    $data['image']= $cap['image'];
    

    to

    $this->data['image']= $cap['image'];
    

    If that doesn't work then make sure the captcha directory you are saving the image to ('img_path' => './captcha/') is writable, either 666 or 777. You can do this be doing chmod 777 captcha/ on the command line. Also make sure that the GD image library extension is enabled in your php.ini

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效