Welcome Guest, Not a member yet? Register   Sign In
Captcha in CI?
#1

[eluser]littlejim84[/eluser]
Hello... First off, I'm really new here... I've only started using CodeIgniter about a day or two ago...

Sorry, this must of been asked a million times but what is the best (and simplest) way to put captcha into a form? I have an image upload that I've managed to get working, which I'm happy with... But it does need captcha...

I've looked in the Wiki and tried to understand the captcha thing in there, but I just don't seem to be able to get it working with my form...

Can anyone explain further? ... Anyhow, I'm sure CodeIgniter had a captcha thing in it before? (or is that my imagination)
#2

[eluser]Jamie Rumbelow[/eluser]
Nope, you're not imagining things! CodeIgniter does include a pretty good CAPTCHA plugin, you can find it in your system/plugins directory.

You load it just like any other library/helper, with the $this->load->plugin(); Then just use the create_captcha function to make one!

Here is the usage guide taken directly from the plugin!

Quote:/*
Instructions:

Load the plugin using:

$this->load->plugin('captcha');

Once loaded you can generate a captcha like this:

$vals = array(
'word' => 'Random word',
'img_path' => './captcha/',
'img_url' => 'http://www.your-site.com/captcha/',
'font_path' => './system/texb.ttf',
'img_width' => '150',
'img_height' => 30,
'expiration' => 7200
);

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


NOTES:

The captcha function requires the GD image library.

Only the img_path and img_url are required.

If a "word" is not supplied, the function will generate a random
ASCII string. You might put together your own word library that
you can draw randomly from.

If you do not specify a path to a TRUE TYPE font, the native ugly GD
font will be used.

The "captcha" folder must be writable (666, or 777)

The "expiration" (in seconds) signifies how long an image will
remain in the captcha folder before it will be deleted. The default
is two hours.

RETURNED DATA

The create_captcha() function returns an associative array with this data:

[array]
(
'image' => IMAGE TAG
'time' => TIMESTAMP (in microtime)
'word' => CAPTCHA WORD
)

The "image" is the actual image tag:
<img src="http://your-site.com/captcha/12345.jpg" width="140" height="50" />

The "time" is the micro timestamp used as the image name without the file
extension. It will be a number like this: 1139612155.3422

The "word" is the word that appears in the captcha image, which if not
supplied to the function, will be a random string.


ADDING A DATABASE

In order for the captcha function to prevent someone from posting, you will need
to add the information returned from create_captcha() function to your database.
Then, when the data from the form is submitted by the user you will need to verify
that the data exists in the database and has not expired.

Here is a table prototype:

CREATE TABLE captcha (
captcha_id bigint(13) unsigned NOT NULL auto_increment,
captcha_time int(10) unsigned NOT NULL,
ip_address varchar(16) default '0' NOT NULL,
word varchar(20) NOT NULL,
PRIMARY KEY (captcha_id),
KEY (word)
)


Here is an example of usage with a DB.

On the page where the captcha will be shown you'll have something like this:

$this->load->plugin('captcha');
$vals = array(
'img_path' => './captcha/',
'img_url' => 'http://www.your-site.com/captcha/'
);

$cap = create_captcha($vals);

$data = array(
'captcha_id' => '',
'captcha_time' => $cap['time'],
'ip_address' => $this->input->ip_address(),
'word' => $cap['word']
);

$query = $this->db->insert_string('captcha', $data);
$this->db->query($query);

echo 'Submit the word you see below:';
echo $cap['image'];
echo '&lt;input type="text" name="captcha" value="" /&gt;';


Then, on the page that accepts the submission you'll have something like this:

// First, delete old captchas
$expiration = time()-7200; // Two hour limit
$DB->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);

// Then see if a captcha exists:
$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND date > ?";
$binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
$query = $this->db->query($sql, $binds);
$row = $query->row();

if ($row->count == 0)
{
echo "You must submit the word that appears in the image";
}

*/
#3

[eluser]littlejim84[/eluser]
Cheers for that... I think the one in the WIKI is not correct? Not sure... But got it working now. Thanks.

One thing though... The captcha plugin creates ALOT of captcha image files... What's the best way to get rid of them after their not in use?

Many thanks for your time on this issue...
#4

[eluser]Jamie Rumbelow[/eluser]
Try deleting the CAPTCHA image after the user get's validated. It will mean a lot of server work every time you post the form, but It will do the job.
#5

[eluser]littlejim84[/eluser]
Hello... yes, this is what I was thinking, using unlink('./captcha/'<file name>) to do it... thing is, how do i get the filename of the image made by the captcha? The image file created looks something like this: 1210635348.87.jpg and I'm not sure how this name is generated? Looks like the timestamp, but has a .87 (or a different number or no number at all) at the end, before the .jpg.

Is there anyway that the captcha plugin returns the actual filename of the file created?

(Sorry if it's really obvious)
#6

[eluser]Jamie Rumbelow[/eluser]
The create_captcha() function returns an associative array with this data:

Quote:[array]
(
"image" => IMAGE TAG
"time" => TIMESTAMP (in microtime)
"word" => CAPTCHA WORD
)

The "image" is the actual image tag:
http://your-site.com/captcha/12345.jpg

The "time" is the micro timestamp used as the image name without the file
extension. It will be a number like this: 1139612155.3422

The "word" is the word that appears in the captcha image, which if not
supplied to the function, will be a random string.
#7

[eluser]kwhitaker[/eluser]
Hrm, need a little captcha help myself.

I've inputted the code according to the example in the plugin file, and I've created a http://www.url.com/images/captcha directory, and given it 777 access. However, when I attempt to load the page, I get the following error:

Code:
A Database Error Occurred

Error Number: 1048

Column 'captcha_time' cannot be null

INSERT INTO captcha (captcha_id, captcha_time, ip_address, word) VALUES ('', NULL, '10.0.0.61', NULL)

The code I am using is:
Code:
&lt;?
$captcha_vals = array('img_path' => './captcha/','img_url' => 'http://dev.vpi-is01/images/captcha/');

$cap = create_captcha($captcha_vals);

$captcha_data = array('captcha_id'    => '',    'captcha_time'    => $cap['time'], 'ip_address'    => $this->input->ip_address(), 'word'=> $cap['word']);

$query_captcha = $this->db->insert_string('captcha', $captcha_data);
$this->db->query($query_captcha);
?&gt;

<li>
                    &lt;?=$cap['image']?&gt;
                </li>
                <li>
                    &lt;input type="text" name="captcha" value="" /&gt;
                </li>

Any thoughts?
#8

[eluser]fusionblu[/eluser]
Try Recaptcha. Worked pretty good for me. Very simple to integrate too.
link - http://recaptcha.net/
#9

[eluser]psycho-vnz[/eluser]
[quote author="littlejim84" date="1210693707"]Hello... yes, this is what I was thinking, using unlink('./captcha/'<file name>) to do it... thing is, how do i get the filename of the image made by the captcha? The image file created looks something like this: 1210635348.87.jpg and I'm not sure how this name is generated? Looks like the timestamp, but has a .87 (or a different number or no number at all) at the end, before the .jpg.

Is there anyway that the captcha plugin returns the actual filename of the file created?

(Sorry if it's really obvious)[/quote]

Hi littlejim84, to solve this open the captcha_pi.php file, and in the line 343 replace:
Code:
$img_name = $now.'.jpg';

By
Code:
$img_name = intval($now).'.jpg';
#10

[eluser]Nonox[/eluser]
Hi!
I followed your advices and I have the captcha plug-in working well, but I have a question, Where is the correct place for the unlink function? I put it begin to load the image and after to load the image in the view and the result was the same, the image doesn't appears. What do you think? How do you manage this?

This is the code of my controller:
Code:
function registro()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->plugin('captcha');

        $vals = array (

            'word'         => 'Random word',

            'img_path'     => './captcha/',

            'img_url'     => base_url().'captcha/',

            'img_width'     => '150',

            'img_height' => 30,

            'expiration' => 7200

        );

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

        //If I uncommet this the image doesn't appears
        //unlink(realpath('captcha').'/'.$cap['time'].'.jpg');

        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        $this->form_validation->set_rules('test', 'test', 'required|trim|max_length[1]|xss_clean|numeric|integer');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('site/miembros/form_registro',$datos_frm);

            //If I uncommet this the image doesn't appears
            //unlink(realpath('captcha').'/'.$cap['time'].'.jpg');
        }
        else
        {
            echo "Form success!";
        }
    }




Theme © iAndrew 2016 - Forum software by © MyBB