mewebstudio / captcha

Captcha for Laravel 5/6/7/8/9/10/11
MIT License
2.46k stars 452 forks source link

Captcha validation returns always wrong captcha error #153

Open MoslemCherif opened 5 years ago

MoslemCherif commented 5 years ago

I could display the captcha, everything is fine, until I submit, there comes the validation error "Wrong Captcha". After debugging, I saw that in the class Mews\Captcha\Captcha, there is a session value clearing $this->session->remove('captcha'); which leads to this problem. As a suggestion, the session key should be removed after successful result I am using Laravel 5.1 and PHP version 5.6

mauricius commented 5 years ago

That's the point of a Captcha code. You don't want to show the same string if the validation fails, otherwise your captcha can be cracked easily using a brute force approach.

If you're submitting the form using a normal POST request you should see a new code after each validation failure. Otherwise, if you are using AJAX requests you have to make sure that the captcha image is refreshed after the failed validation.

Something like this should work for you

<img id="captcha" src="{{ Captcha::src() }}" alt="captcha">

And the jQuery code that handles the refresh

var source = $("#captcha").attr('src');

$("#captcha").attr('src', source + '?timestamp=' + new Date().getTime());