1

I know this topic is existing, but no one could solve my problem.
I am using the library mewebstudio/captcha, everything is fine, but when I submit, it tells me always that the captcha is wrong.

My validation rules:

'captcha' => 'required|captcha'

In my html form:

<?= captcha_img(); ?>
<input type="text" name="captcha">



I am using php5.6, Laravel 5.1 and mews/captcha 2.2

3 Answers 3

1

My solution:

'captcha' => ['required' , new CheckCaptcha]

Rule class :

public function passes($attribute, $value)
{
    return Hash::check($value, session('captcha.key'));
}

/**
 * Get the validation error message.
 *
 * @return string
 */
public function message()
{
    return 'Invalid captcha';
}
0

in CaptchaServiceProvider.php in package vendor, comment routes:

if ((double)$this->app->version() >= 5.2) {
            $router->get('captcha/api/{config?}', '\Mews\Captcha\CaptchaController@getCaptchaApi')->middleware('web');
            $router->get('captcha/{config?}', '\Mews\Captcha\CaptchaController@getCaptcha')->middleware('web');
        } else {
            $router->get('captcha/api/{config?}', '\Mews\Captcha\CaptchaController@getCaptchaApi');
            $router->get('captcha/{config?}', '\Mews\Captcha\CaptchaController@getCaptcha');
        }

then add routes above in your routes file witout middleware('web'):

Route::get('captcha/api/{config?}', '\Mews\Captcha\CaptchaController@getCaptchaApi');
Route::get('captcha/{config?}', '\Mews\Captcha\CaptchaController@getCaptcha');
-1

I found a solution, I hope that will help someone in the future.
Well it is a bug in the library itself, where they delete the session key without checking the result:

public function check($value)
{
    if ( ! $this->session->has('captcha'))
    {
        return false;
    }

    $key = $this->session->get('captcha.key');
    $sensitive = $this->session->get('captcha.sensitive');

    if ( ! $sensitive)
    {
        $value = $this->str->lower($value);
    }

    $this->session->remove('captcha');

    return $this->hasher->check($value, $key);
}

So my solution is like that (you need to extend the class, override the method and bind the class to the new implementation):

public function check($value)
{
    if ( ! $this->session->has('captcha'))
    {
        return false;
    }

    $key = $this->session->get('captcha.key');
    $sensitive = $this->session->get('captcha.sensitive');

    if ( ! $sensitive)
    {
        $value = $this->str->lower($value);
    }

    $isNotARobot = $this->hasher->check($value, $key);dd($isNotARobot);

    if ($isNotARobot) {
        $this->session->remove('captcha');
    }
    return $isNotARobot;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.