Captcha Code and Validation Example using Botdetect Package in Laravel

How to Generate Captcha Code in Laravel is the topic, we will discuss today. Captcha stands for Completely Automated Public Turing test. It is mainly used as a security test to ensure only human users can pass through. Computers or bots are not able of solving a captcha.

There are different types of captcha we can use some protection. The logic behind why websites implement CAPTCHA codes into their registration processes is because of spam. In this post i show you very simple and from scratch of generate captcha code image for your laravel app. 

captcha-code-and-validation-example-in-laravel

Step 1: Configure Laravel Project

Install the new Laravel Project by the running following command.

composer create-project --prefer-dist laravel/laravel laravelcaptcha

Step 2: Install Captcha package

In first step we will install captcha-com/laravel-captcha package for generate captcha code image. this package through we can generate captcha code image for our project. so first fire bellow command in your cmd or terminal:

composer require captcha-com/laravel-captcha:"4.*"

Now we need to add provider path and alias path in config/app.php file so open that file and add bellow code.

return [
	$provides => [
		LaravelCaptcha\Providers\LaravelCaptchaServiceProvider::class
	],

Now we will run bellow command that way it will generate app/captcha.php file for configration and we can change and customize easily.

php artisan vendor:publish

Step 3: Setup Controller Method

We will use Laravel default auth mechanism to complete our project. So we have no need to create new controller. So go to your app/Http/Controllers/Auth/RegisterController.php. In your register controller your default validator function will be look like this. 

 protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'slug' => 'required',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|min:6|dumbpwd|confirmed'
        ]);
    }

Now you have to just add one line code. see the below code

 protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'slug' => 'required',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|min:6|dumbpwd|confirmed',
            'CaptchaCode' => 'required|valid_captcha'
        ]);
    }

Look we just added 'CaptchaCode' => 'required|valid_captcha' line to get Capctcha code image. 

Step 4: Create Blade File

This is the last step. So go to your resources/views/auth/register.blade.php and add the following code to your register form.

Finally, Our How to Generate Captcha Code in Laravel is over. Hope it will work for you. 

Codechief is a very fast growing community among programmers and have a reach of around 1 million+ readers globally. Contribution at Codechief is open for all those who have a passion to learn and help others by sharing their knowledge. If you think you have the zeal to learn, start contributing on Codechief contribute. you can also mail your article to [email protected] See your article appearing on the codechief main page and help other code.

We believe that everyone has the right to learn, so we allow both students and professionals to contribute on Codechief.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using JavaScript, Laravel & Wordpress specifically. Facebook Github Website