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.
<?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 :
- Create folder captcha on root with permission 777
- In config.php
$config['base_url'] = 'http://localhost/yourproject/';
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
Image Example 2
Location: ./application/controllers/Captcha.php
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Captcha extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->driver("session");
$this->load->helper(array('form', 'url', 'captcha'));
}
public function index() {
$this->form_validation->set_rules('name', "Name", 'required');
$this->form_validation->set_rules('captcha', "Captcha", 'required');
$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');
$name = set_value('name');
$data = array('name' => $name);
$this->load->view('success-view', $data);
} else {
$vals = array('img_path' => 'static/','img_url' => base_url().'static/');
$captcha = create_captcha($vals);
$this->session->set_userdata('captchaWord', $captcha['word']);
$this->load->view('captcha-view', $captcha);
}
}
}
Location: *./application/views/captcha-view.php /
Add a Captcha!
<h1>Adding a captcha</h1>
<p>Take a look at <code style="background:rgb(220,220,220);">application/controllers/Captcha.php</code> to look at the controller used to generate the captcha.</p>
<?php echo validation_errors(); ?>
<?php echo form_open( 'captcha'); ?>
</p>
<p>
<label for="name">Name:</label>
<input id="name" name="name" type="text" />
</p>
<?php echo $image; ?>
<p>
<label for="name">Captcha:</label>
<input id="captcha" name="captcha" type="text" />
</p>
<?php echo form_submit( "submit", "Submit"); ?>
<?php echo form_close(); ?>
Location: ./application/views/success-view.php
<html>
<head>
<title>Success!</title>
</head>
<body>
<h1>Success!</h1>
<p>Thanks, <?php echo $name; ?>!</p>
</body>
</html>