linuxoid
linuxoid

Reputation: 1447

Securimage captcha - multiple forms validate randomly

I have multiple forms on a page which seem to validate strangely or randomly.

  1. If I enter the first code correctly, the same form will validate correctly every time until the code is entered incorrectly. After this validation always fails until I click on the image to reload it, then it all starts fine.
  2. Regardless of the 1st form processed, any other form fails validation until the image is clicked to reload. Then it start passing as in point 1. But all other forms will fail.

Namespaces don't seem to help.

This is the view.php:

$captcha->setNamespace($bUID);
$captcha->display();
echo $form->text('code'.$bUID, $code);

This is the controller.php:

$captcha->setNamespace($_POST['buid']);
if (!$captcha->check('code')) {
    array_push($this->form_errors, $this->error_code);
}

The $bUID is a unique form block number, that is each form field is unique.

Looks like even though each form is unique, the Securimage image is created one for all, not as one for each form. And the namespaces don't help for some reason.

Is there any way to create a captcha image with a unique code and path for each unique form? Or am I not using the namespaces correctly?

Thank you.

Upvotes: 0

Views: 500

Answers (1)

Son of Father
Son of Father

Reputation: 111

Try use also image_id option besides namespace.

Here code, which I use for multicaptcha purposes:

require_once 'inc/securimage/securimage.php';

define('SECURIMAGE_OPTIONS', array(
    'securimage_path' => 'inc/securimage',
    'input_text' => 'Введите проверочный код',
    'show_audio_button' => false
));

function display_captcha($identity){
    echo Securimage::getCaptchaHtml(
        array_merge(
            SECURIMAGE_OPTIONS,
            array("image_id"=>$identity,
                "namespace"=>$identity,
            )
        )
    );

    $input_attrs = array();
    $input_attrs['type'] = 'hidden';
    $input_attrs['name'] = 'namespace';
    $input_attrs['id']   = 'namespace';
    $input_attrs['value'] = $identity;
    //with captcha component I add tag <input type="hidden" name="namespace"  ... /> for using in validation script 
    $input_attr = '';
    foreach($input_attrs as $name => $val) {
        $input_attr .= sprintf('%s="%s" ', $name, htmlspecialchars($val));
    }
    echo(sprintf('<input %s/>', $input_attr));
}

/*
 * Now you can use multiple captchas in forms:  with names: captcha1
 * <?php display_captcha("captcha1"); ?>
 *
 * And valudation code:
 * $image = new Securimage();
 * if(isset($_POST['namespace'])){
 *   $namespace = $_POST['namespace'];
 *   $image->setNamespace($namespace);
 * }
 * if (!$image->check($_POST['captcha_code'])) {
 *  ....
 * }
 * */

Upvotes: 0

Related Questions