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.
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
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
Image Example 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With