P.Yntema P.Yntema - 2 months ago 21
PHP Question

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.

Answer
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Mycaptcha extends CI_Controller  {

public function __construct() {

    parent::__construct();  
        $this->load->helper('captcha');
        $this->load->helper('url');
    }

    public function index() { 
        $vals = array(
            'img_path' => './captcha/',
            'img_url' => base_url().'/captcha/',
        );

        $captcha = create_captcha($vals);
        $captcha_image = $captcha['image'];

        print_r($captcha);
    }
}

Make sure you have :

  1. Create folder captcha on root with permission 777
  2. In config.php $config['base_url'] = 'http://localhost/yourproject/';
Comments