How to build your own Captcha for security in Laravel — SS Blog

How to build your own Captcha for security in Laravel — SS Blog

To stop brute force attacks into our application captcha is needed.
Most of the developers use google services for this security. But still, there are many developers who did not trust the third-party services for security purposes they build their own captcha.

mewebstudio/captcha Laravel package is very simple and easy to integrate in our project.

composer require mews/captcha
In the root directory of your laravel project run the above command in the terminal to install the package. And then configure the package with the following steps.
Open the config/app.php file and add this line
'Mews\Captcha\CaptchaServiceProvider', Into the providers array.
And this
'Captcha' => 'Mews\Captcha\Facades\Captcha',

Into the aliases array in the same file.
After this run the below command on the terminal :
php artisan vendor:publish
Now all the vendor files will be published. config/captcha.php file is now generated.
To refresh the generated captcha add routes for that in the web.php file.
Route::get('refresh-captcha',
'ContactController@refreshCaptcha')->name('refreshCaptcha');

In the controller add the below line
use Captcha;
To validate captcha you can use the following code in your form submission code
$this->validate($request, [
'captcha' => 'required|captcha', // validate captcha]);

After this you can write your code that will run if your captcha is validated.
public function refreshCaptcha()
{
return response()->json([
'captcha' => Captcha::img()
]);
}

The above code is used to refresh the captcha if needed.
Now in the view file, you can add the HTML for the captcha image.

Captcha::img() this method will help us to load the captcha image.

And for the refresh captcha button, you can use ajax that will refresh the captcha.
$(document).ready(function() {
$('.refreshCaptcha).click(function() {
$.ajax({
type: 'get',
url: '{{ route('refreshCaptcha') }}',
success:function(data) {
$('.captcha-image').html(data.captcha);
}
});
});
});

After following all the above-mentioned steps you are able to use captcha while submitting the web forms.

The English language is the most widely used medium of communication around the world. Having a certification for the English language can be an advantage. StudySection provides an English Certification Exam that tests English language proficiency in English grammar, reading, and writing.

Originally published at https://studysection.com on November 24, 2021.

--

--

--

The most loved online platform for eCertification in several subjects including but not limited to Software Development, Aptitude and more.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Study Section

Study Section

The most loved online platform for eCertification in several subjects including but not limited to Software Development, Aptitude and more.

More from Medium

Laravel 8 Datatables Keeping Selected Page Number

Laravel Advance | Laravel Bail Rule | Stop Validation On First Failure

How to Create Own Cache driver and Learn Cache drivers from Laravel

Laravel Passport API that authenticates email or phone number & password