Question

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.

Était-ce utile?

La solution

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top