[eluser]dinhtrung[/eluser]
I tried to write a comment widget, which will display a comment form.
For guest (not logged in), I use 2 flashdata (captcha_word and captcha_time) to check for Captcha validation. But it just won't work.
I read the plugin code and see that it create a new object of current CI. Is it the reason why validation using current object and new created object failed???
I tried to read the logs file, but it just generate too many lines and I gave up.
The widget is something like this:
Code:
/* File : widgets/comment_form.php */
class Comment_form extends widget
{
function run()
{
$captcha_config = $this->config->item('captcha', 'form');
$this->load->plugin('captcha');
$captcha = create_captcha($captcha_config);
$this->session->set_flashdata('captcha_word', $captcha['word']);
$this->session->set_flashdata('captcha_time', $captcha['time']);
$this->render('form_captcha', $captcha);
}
}
/* File : widgets/views/form_captcha.php */
<?php
echo form_open('comments_handle/create');
echo form_label('Name');
echo form_input('name');
echo form_label('Email');
echo form_input('email');
echo form_textarea('body');
echo $image;
echo form_input('captcha');
echo form_submit('OK');
echo form_close();
?>
/* File : controllers/comments_handle.php */
...
function _valid_captcha($code = FALSE)
{
if ($code == FALSE) $code = $this->input->post('captcha', TRUE);
$config = $this->config->item('captcha', 'form');
$time = $this->session->userdata('captcha_time');
$word = $this->session->userdata('captcha_word');
list($usec, $sec) = explode(" ", microtime());
$now = ((float)$usec + (float)$sec);
if ($now - $time > $config['expiration']) {
log_message("debug", "Validation for Captcha expired. Now is $now and Time is $time.");
return FALSE;
}
if ($code != $word) {
log_message("debug", "Validation for Captcha differs.");
return FALSE;
}
return TRUE;
}
Please give me some advice. Thanks.