我的codeigniter验证码不生成图像

时间:2014-03-23 10:26:37

标签: php codeigniter captcha

我的CodeIgniter验证码没有生成,所以我无法在我的视图中显示它

控制器

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); 
}

查看

    <?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 ?>

据我所知,我没有验证码文件夹或字体文件夹,是否仍然会在没有这2个文件夹的情况下自行生成?我已经尝试了从一些解决的问题,但它仍然无法正常工作。没有错误,但验证码图像没有出现。

请帮帮我。

1 个答案:

答案 0 :(得分:4)

问题是您将$this->data传递到视图,但是您要将生成的图像分配给$data。所以你需要从

改变它
$data['image']= $cap['image'];

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

如果不起作用,请确保将图像保存到('img_path' => './captcha/')的验证码目录是可写的,可以是666或777.您可以对命令执行chmod 777 captcha/线。还要确保在php.ini

中启用了GD图像库扩展程序