How to integrate GooglereCAPTCHA in Laravel5 forms

Source: Internet
Author: User
How to integrate GooglereCAPTCHA verification code in Laravel5 forms 1. Introduction

Sometimes we need to use verification codes when submitting forms to prevent malicious operations such as irrigation and robots. There are many open-source libraries for verification codes, currently, Google reCAPTCHA, the most frequently used one, is easy to use on both the client and server. So here we use Google reCAPTCHA as an example to demonstrate how to embed a verification code in a Laravel application form.

Github has a ready-made project integrating Google reCAPTCHA to Laravel: anhskohbo/no-captcha. In this tutorial, we will demonstrate how to use the verification code in Laravel 5.

2. installation and configuration

We use Composer to install the extension package:

composer require anhskohbo/no-captcha 2.*

After the installation is complete, register the service provider in config/app. php to the providers array:

Anhskohbo\NoCaptcha\NoCaptchaServiceProvider::class

Then you need to obtain the Google recaptcha site key and secret-key for the site, log on to Recaptcha Admin, and register the site for the first time:

Click "Register" to obtain the site key and secret key corresponding to the site:

Add the obtained site key and secret key to the. env file:

NOCAPTCHA_SECRET=[secret-key]NOCAPTCHA_SITEKEY=[site-key]

In this way, we have configured recaptcha for the Laravel application. the verification code is displayed in the form below.

3. integrate the verification code into the form

To display the verification code in the view, insert the following line of code:

{!! app('captcha')->display(); !!}

First, we define an access route in routes. php:

Route::get('contact', function() {    return View::make('contact');});Route::post('contact', 'EnquiryController@index');

Then we define a controller EnquiryController:

  '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.}}}

Finally, create a view file resources/views/contact. blade. php and edit the file as follows:

Contact us

@if (count($errors) > 0)

Whoops! There were some problems with your input.

    @foreach ($errors->all() as $error)
  • {{ $error }}
  • @endforeach

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

Name

{!! Form::text('name','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Your Full Name')) !!}

E-Mail Address

{!! Form::text('email','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Your Email')) !!}

Subject

{!! Form::text('subject','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Your Subject')) !!}

Message

{!! Form::textarea('msg','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Your Full Name')) !!}

Captcha

{!! app('captcha')->display(); !!}

Submit

Access the http://laravelacademy.org/contact in a browser and the effect is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.