1 頁 (共 1 頁)

How to use captcha in Laravel 5

發表於 : 2015-12-06 22:02:29
yehlu
http://tutsnare.com/how-to-use-captcha-in-laravel-5/

How to use captcha in Laravel 5 – Google reCAPTCHA

Sometimes we should use captcha on our form to prevent spams, bots etc. There is also more captcha open source library, mostly used library is
Google reCAPTCHA. Which is freely and easy to use for both client and server side. So we are using google recaptcha to embed on form of laravel application. For use captcha in laravel 5 there is also inbuilt libraries on github to use google recapcha in laravel 5 application. I found a good package for captcha in laravel on github with named anhskohbo/no-captcha. In this artcle how to use captcha in Laravel 5, i am working with this package to include captcha on form. Let’s see how to use captcha in laravel 5 with package anhskohbo-no-captcha(Google reCAPTCHA).

google-recapcha

What is captcha :- A program that use for human verification or program which protect websites against bots, spam etc. Generally Captcha contain a image for human challenge on forms for human read and enter captcha image text.

Steps to add captcha in laravel 5

1. include package :

a. Add the below code to the require section of composer.json.

"require": {
// More already here.
"anhskohbo/no-captcha": "2.*",
},
b. Now open your terminal/cmd and and run below command to get package from github.

composer update
composer-update

c. After run composer update add ServiceProvider to the providers array in config/app.php.

'providers' => [
'Anhskohbo\NoCaptcha\NoCaptchaServiceProvider',
],

// Or upper version.

'providers' => [
Anhskohbo\NoCaptcha\NoCaptchaServiceProvider::class,
],
2. Get google recaptcha site key and secret-key : After config we need to get recaptcha key and secret for our site. For this Got to Recaptcha Admin. Log in if not already, Then click on “Get recaptcha” Button.
Add your domain to “Domains” of “Key settings” section/form and click on save. This will generate a “site key” and “secret key”.

3. Add Keys to .env : Now add below code to your .env file. and paste your keys here.

NOCAPTCHA_SECRET=[secret-key]
NOCAPTCHA_SITEKEY=[site-key]
So Now our recaptcha config for laravel 5 application has been done. Let’s see how to integrate captcha on form.

How to use captcha and validate in laravel 5

To display captcha on view add below code to your name.blade.php

{!! app('captcha')->display(); !!}
Example of a contact form recaptcha in laravel 5:-

How to use captcha in laravel 5

View : Create a view file under resources/views/contact.blade.php. And add below code

<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Contact us</div>
<div class="panel-body">
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br /><br />
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif

{!! Form::open(array('url'=>'contact','method'=>'POST', 'id'=>'myform')) !!}

<div class="form-group">
<label class="col-md-4 control-label">Name</label>
<div class="col-md-6">
{!! Form::text('name','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Your Full Name')) !!}
</div>
</div>

<div class="form-group">
<label class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
{!! Form::text('email','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Your Email')) !!}
</div>
</div>

<div class="form-group">
<label class="col-md-4 control-label">Subject</label>
<div class="col-md-6">
{!! Form::text('subject','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Your Subject')) !!}
</div>
</div>

<div class="form-group">
<label class="col-md-4 control-label">Message</label>
<div class="col-md-6">
{!! Form::textarea('msg','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Your Full Name')) !!}
</div>
</div>

<div class="form-group">
<label class="col-md-4 control-label">Captcha</label>
<div class="col-md-6">
{!! app('captcha')->display(); !!}
</div>
</div>

<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Submit
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
Controller :- Now create a controller file under “Http/controllers/EnquiryController.php”. And add below code.

<?php namespace App\Http\Controllers;
use Input;
use Validator;
use Redirect;
use Session;
class EnquiryController extends Controller {
public function index()
{
$data = Input::all();
$rules = array(
'name' => 'required',
'email' => 'required|email',
'subject' => 'required',
'g-recaptcha-response' => 'required|captcha',
'msg' => 'required',
);
$validator = Validator::make($data, $rules);
if ($validator->fails()){
return Redirect::to('/contact')->withInput()->withErrors($validator);
}
else{
// Do your stuff.
}
}
}
Routes : Now add your contact routes in “Http/routes.php”.

Route::get('contact', function() {
return View::make('contact');
});
Route::post('contact', 'EnquiryController@index');
Now run your contact form url “http://localhost/laravel/contact”. You will see your google recapcha working here. Now check your captcha validation. So using laravel 5 captcha package it’s easy to use captcha in laravel 5. For more about how to use captcha in Laravel 5 using anhskohbo/no-captcha.