onlinecode

Laravel – Generate Captcha code and Validation example using BotDetect package

Laravel – Generate Captcha code and Validation example using BotDetect package

In this post we will give you information about Laravel – Generate Captcha code and Validation example using BotDetect package. Hear we will give you detail about Laravel – Generate Captcha code and Validation example using BotDetect packageAnd how to use it also give you demo for it if it is necessary.

I think we should use captcha code on our registration form because captcha code prevent spams, bots etc. Most of the application we need to use captcha varification because it very important for security reason. There are several library to generate captcha image in Laravel. In this example i use BotDetect package library for generate captcha image and using their validation. BotDetect is very popular library and very customize library. they provide several ui design for captcha code and also how much character do you want?, you can set all the thing using this package.

In this post i give you very simple and from scratch of generate captcha code image as you can also see bellow preview. After complete this example you can found ui design like bellow preview, you have to just follow few step and find result:

Preview:

Step 1: Installation

In first step we will install captcha-com/laravel-captcha package for generate captcha code image. this package through we can generate 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.

config/app.php

return [

......

$provides => [

......

......,

LaravelCaptchaProvidersLaravelCaptchaServiceProvider::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 2: Add Route

In second step we will add new two route for creating small example that way we can undestand very well. so first add bellow route in your routes.php file.

app/Http/routes.php

Route::get('web-register', 'AuthAuthController@webRegister');

Route::post('web-register', 'AuthAuthController@webRegisterPost');

Also see:Laravel – Google reCaptcha Code using anhskohbo/no-captcha

Step 3: Add Controller Method

In this step we will add webRegister() and webRegisterPost() method in AuthController Controller file for our example. If you want to copy whole controller file then also you can just copy and paste.

app/Http/Controllers/Auth/AuthController.php

namespace AppHttpControllersAuth;


use Validator;

use AppHttpControllersController;

use IlluminateFoundationAuthThrottlesLogins;

use IlluminateFoundationAuthAuthenticatesAndRegistersUsers;

use Auth;

use IlluminateHttpRequest;


class AuthController extends Controller

{


use AuthenticatesAndRegistersUsers, ThrottlesLogins;


protected $redirectTo = '/';


/**

* Create a new authentication controller instance.

*

* @return void

*/

public function __construct()

{

$this->middleware('guest', ['except' => 'logout']);

}


/**

* Get a validator for an incoming registration request.

*

* @param array $data

* @return IlluminateContractsValidationValidator

*/

protected function validator(array $data)

{

return Validator::make($data, [

'name' => 'required|max:255',

'email' => 'required|email|max:255|unique:users',

'password' => 'required|confirmed|min:6',

]);

}


/**

* Create a new user instance after a valid registration.

*

* @param array $data

* @return User

*/

protected function create(array $data)

{

return User::create([

'name' => $data['name'],

'email' => $data['email'],

'password' => bcrypt($data['password']),

]);

}


public function webRegister()

{

return view('webRegister');

}


public function webRegisterPost(Request $request)

{

$this->validate($request, [

'name' => 'required',

'email' => 'required|email',

'password' => 'required|same:password_confirmation',

'password_confirmation' => 'required',

'CaptchaCode' => 'required|valid_captcha'

]);

print('write your other code here.');

}

}

Step 4: Add Blade file


This is a last step and you have to just create new blade file webRegister.blade.php and put bellow code on that file.

resources/views/webRegister.blade.php

Also see:Laravel – Google reCaptcha Code using anhskohbo/no-captcha

@extends('layouts.app')


@section('content')

<link href="{{ captcha_layout_stylesheet_url() }}" type="text/css" rel="stylesheet">

<div >

<div >

<div >

<div >

<div >Register</div>

<div >

<form role="form" method="POST" action="{{ url('/web-register') }}">

{!! csrf_field() !!}


<div >

<label >Name</label>

<div >

<input type="text" name="name" value="{{ old('name') }}">

@if ($errors->has('name'))

<span >

<strong>{{ $errors->first('name') }}</strong>

</span>

@endif

</div>

</div>


<div >

<label >E-Mail Address</label>


<div >

<input type="email" name="email" value="{{ old('email') }}">


@if ($errors->has('email'))

<span >

<strong>{{ $errors->first('email') }}</strong>

</span>

@endif

</div>

</div>


<div >

<label >Password</label>


<div >

<input type="password" name="password">


@if ($errors->has('password'))

<span >

<strong>{{ $errors->first('password') }}</strong>

</span>

@endif

</div>

</div>


<div >

<label >Confirm Password</label>


<div >

<input type="password" name="password_confirmation">


@if ($errors->has('password_confirmation'))

<span >

<strong>{{ $errors->first('password_confirmation') }}</strong>

</span>

@endif

</div>

</div>


<div >

<label >Captcha</label>


<div >

{!! captcha_image_html('ContactCaptcha') !!}

<input type="text" id="CaptchaCode" name="CaptchaCode" style="margin-top:5px;">


@if ($errors->has('CaptchaCode'))

<span >

<strong>{{ $errors->first('CaptchaCode') }}</strong>

</span>

@endif

</div>

</div>


<div >

<div >

<br/>

<button type="submit" >

<i ></i>Register

</button>

</div>

</div>

</form>

</div>

</div>

</div>

</div>

</div>

@endsection

Try this.. if you want to more custom captcha then you can do from here : captcha.com.

Hope this code and post will helped you for implement Laravel – Generate Captcha code and Validation example using BotDetect package. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version