Laravel 5 How to integrate with Google ReCAPTCHA verification code in forms

Source: Internet
Author: User

1. Introduction

Sometimes we need to use verification code in the form submission to prevent flooding, bots and other malicious operations, about the verification code has a lot of open source Library to use, the current most used is the Google recaptcha--whether the client or server-side use is very simple and convenient, So here we take Google reCAPTCHA as an example of how to embed a verification code in a form that is laravel applied.

There are ready-to-integrate Google reCAPTCHA to Laravel projects on GitHub: Anhskohbo/no-captcha. In this tutorial we will demonstrate how to use the Captcha in Laravel 5.

2. Installation & Configuration

We use composer to install the expansion pack:

Composer require Anhskohbo/no-captcha 2.*

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

Anhskohbo\nocaptcha\nocaptchaserviceprovider::class

Then need to get Google reCAPTCHA site key and Secret-key for the website, login reCAPTCHA Admin, first get need to register the site:

Click "Register" to get the site key and secret key for the website:

Add the site key and secret key you just acquired to the. env file:

Nocaptcha_secret=[secret-key]nocaptcha_sitekey=[site-key]

So we've configured the reCAPTCHA for the Laravel app, and we'll show the verification code in the form below.

3. Integrated verification code in the form

To display the verification code in the view, you need to 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::p ost (' Contact ', ' enquirycontroller@index ');

Then we define a controller Enquirycontroller:

 
  ' Required ',  ' email ' = ' required|email ', ' subject ' = ' required ', ' g-recaptcha-response ' and ' required| ' Captcha ', ' msg ' = ' required ',); $validator = Validator::make ($data, $rules); if ($validator->fails ()) {    Return redirect::to ('/contact ')->withinput ()->witherrors ($validator);} else{    //Do Your stuff.}}}

Finally we create a view file resources/views/contact.blade.php, and edit its contents 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 (' 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

Accessing the http://laravelacademy.org/contact in the browser shows the following effects:

  • Related Article

    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.