How to create captcha code in Laravel 5?

How to create captcha code in Laravel 5?

In this post we will give you information about How to create captcha code in Laravel 5?. Hear we will give you detail about How to create captcha code in Laravel 5?And how to use it also give you demo for it if it is necessary.

How to create captcha code in Laravel 5?

Today, i want to share with you how to implement simple captcha code with refresh button in laravel 5 app form. In this tutorial you can simply use captcha with validation in your register form or login form as you want. i use mews library package for captcha in laravel 5.5 application, it will help to generate captcha image with proper validation.

As we know well, some time we need to add captcha code for security level, it will improve security of your application. Generally we implement captcha code in register and login form for prevent bots and crawler. So it will help you to integrate captcha code in laravel 5.5 application.

I use mews/captcha composer package for generate captcha code image. they also provide validation for captcha. it is very helpful package. So if you also want to create captcha in your php laravel app then follow bellow step and you will get layout like as bellow:

Preview:

Step 1 : Install Laravel 5 Application

we are going from scratch, So we need to download fresh Laravel 5.5 version application using bellow command, So open your terminal OR command prompt and run bellow command:

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

Step 2: Install mews Captcha Package

Here, we have to add mews Captcha package so one your cmd or terminal and fire bellow command:

composer require mews/captcha

After successfully install package, open config/app.php file and add service provider and alias.

config/app.php

'providers' => [

....

MewsCaptchaCaptchaServiceProvider::class,

],

'aliases' => [

....

'Captcha' => MewsCaptchaFacadesCaptcha::class,

],

Also see:Laravel – Generate Captcha code and Validation example using BotDetect package

Step 3: Create Routes

here, we require to add three routes display login form, post login form and another for refresh captcha code. so open your routes/web.php file and add following route.

routes/web.php

Route::get('my-captcha', 'HomeController@myCaptcha')->name('myCaptcha');

Route::post('my-captcha', 'HomeController@myCaptchaPost')->name('myCaptcha.post');

Route::get('refresh_captcha', 'HomeController@refreshCaptcha')->name('refresh_captcha');


Step 4: Create HomeController Methods

Now we require to create HomeController and add new three methods as myCaptcha(), myCaptchaPost() and refreshCaptcha(). So let’s create controller and then put bellow code:

routes/web.php

<?php


namespace AppHttpControllers;


use IlluminateHttpRequest;


class HomeController extends Controller

{

/**

* Create a new controller instance.

*

* @return void

*/

public function myCaptcha()

{

return view('myCaptcha');

}


/**

* Create a new controller instance.

*

* @return void

*/

public function myCaptchaPost(Request $request)

{

request()->validate([

'email' => 'required|email',

'password' => 'required',

'captcha' => 'required|captcha'

],

['captcha.captcha'=>'Invalid captcha code.']);

dd("You are here :) .");

}


/**

* Create a new controller instance.

*

* @return void

*/

public function refreshCaptcha()

{

return response()->json(['captcha'=> captcha_img()]);

}

}

Step 5: Create View File

At last step, we have to simple create myCaptcha.blade.php file and need to write code there for generate bootstrap login form, jquery ajax code. So let’s copy bellow code:

resources/views/myCaptcha.blade.php

<html lang="en">

<head>

<title>How to create captcha code in Laravel 5?</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">

<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

</head>


<body>

<div style="margin-top: 50px">

<div >

<div >

<div >Login</div>


<div >

<form method="POST" action="{{ route('myCaptcha.post') }}">

{{ csrf_field() }}


<div >

<label for="email" >E-Mail Address</label>


<div >

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


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

<span >

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

</span>

@endif

</div>

</div>


<div >

<label for="password" >Password</label>


<div >

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


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

<span >

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

</span>

@endif

</div>

</div>


<div >

<label for="password" >Captcha</label>


<div >

<div >

<span>{!! captcha_img() !!}</span>

<button type="button" ><i ></i></button>

</div>

<input id="captcha" type="text" placeholder="Enter Captcha" name="captcha">


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

<span >

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

</span>

@endif

</div>

</div>


<div >

<div >

<button type="submit" >

Submit

</button>

</div>

</div>

</form>

</div>

</div>

</div>

</div>

</body>


<script type="text/javascript">


$(".btn-refresh").click(function(){

$.ajax({

type:'GET',

url:'/refresh_captcha',

success:function(data){

$(".captcha span").html(data.captcha);

}

});

});


</script>


</html>

Now we are ready to run our captcha code example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

Also see:Laravel 5 – Simple CRUD Application Using ReactJS – Part 1

http://localhost:8000/my-captcha

You will get more info from here : mewe captcha.

I hope it can help you….

Hope this code and post will helped you for implement How to create captcha code in Laravel 5?. 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

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  38  =  48

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US