Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Captcha not working in CI

I wrote a small piece of code which should work for a captcha in Codeigniter. The code should just simply print the time the captcha was created, for a first try. But it doesn't seem to even create the captcha itself. I'm sure the helper is loaded, this is done in the construct function. Next to that, the correct rights for writing the image to a folder should be there. Anyone any idea why it isn't working as it should?

defined('BASEPATH') OR exit('No direct script access allowed');
class Register extends CI_Controller{

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

    public function generate_captcha(){
        $vals = array(
            'img_path' => './captcha/',
            'img_url' => base_url().'captcha/',
        );
        echo base_url().'assets/images/captcha/';
        $captcha = create_captcha($vals);

        echo 'cap time: ' . $captcha['time'];

        $captcha_image = $captcha['image'];
        return $captcha_image; 
    }

}

Edit Could it have anything to do with something apart from this code? I already set the correct rights to the folder, so it can write images to the directory.

like image 668
P.Yntema Avatar asked Sep 12 '16 18:09

P.Yntema


1 Answers

Create a folder outside of application called captcha Captcha Helper I think you need also to have more $vals in there as well not just the img_path and img_url

Also make sure folder chmod 0777 permissions or 0700

You may need to configure some routes also

$route['register/generate_captcha'] = 'register/generate_captcha';

Filename: Register.php

application

assets > images > captcha // Has the correct permissions

assets > images > captcha > fonts // Has the correct permissions

system

index.php

Controller

Updated

Filename: Register.php following the file and class style guide

Set your base url: $config['base_url'] = 'http://localhost/yourproject/';

<?php

class Register extends CI_Controller {

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

    public function index(){
        $vals = array(
            'word' => 'Random word',
            'img_path' => './assets/images/captcha/',
            'img_url' => base_url('assets/images/captcha'),
            'font_path' => './assets/images/captcha/fonts/texb.ttf',
            'img_width' => '150',
            'img_height' => 30,
            'expiration' => 7200,
            'word_length' => 8,
            'font_size' => 16,
            'img_id' => 'Imageid',
            'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
        );

        $cap = create_captcha($vals);
        echo $cap['image'];

    }

}

Image Example 1

enter image description here

Image Example 2

enter image description here

like image 84
Mr. ED Avatar answered Oct 05 '22 09:10

Mr. ED