Home > Backend Development > PHP Tutorial > Laravel 5 表单中如何集成使用 Google reCAPTCHA 验证码

Laravel 5 表单中如何集成使用 Google reCAPTCHA 验证码

WBOY
Release: 2016-06-23 13:13:54
Original
1651 people have browsed it

1、简介

有时候我们需要在表单提交时使用验证码以防止灌水、机器人等恶意操作,关于验证码有很多开源库可供使用,目前使用率最高的当属Google reCAPTCHA——无论是客户端还是服务器端使用起来都很简单方便,所以这里我们以Google reCAPTCHA为例演示如何在Laravel应用的表单中嵌入验证码。

Github上有现成的集成Google reCAPTCHA到Laravel的项目: anhskohbo/no-captcha 。在这篇教程中我们将演示如何在Laravel 5中使用验证码。

2、安装&配置

我们使用Composer安装该扩展包:

composer require anhskohbo/no-captcha 2.*
Copy after login

安装完成后,我们需要在 config/app.php 中注册服务提供者到 providers 数组:

Anhskohbo\NoCaptcha\NoCaptchaServiceProvider::class
Copy after login

然后需要为站点获取Google recaptcha site key和secret-key,登录 Recaptcha Admin ,初次获取需要先注册站点:

点击“Register”,即可获取站点对应的site key和secret key:

将刚刚获取到的site key和secret key添加到 .env 文件中:

NOCAPTCHA_SECRET=[secret-key]NOCAPTCHA_SITEKEY=[site-key]
Copy after login

这样我们就为Laravel应用配置好了recaptcha,下面我们在表单中显示验证码。

3、在表单中集成验证码

要在视图中显示验证码,需要插入如下这行代码:

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

首先我们在 routes.php 中定义一个访问路由:

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

然后我们定义一个控制器 EnquiryController :

<?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.		}	}}
Copy after login

最后我们创建一个视图文件 resources/views/contact.blade.php ,编辑其内容如下:

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')) !!}
{!! Form::text('name','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Your Full Name')) !!}
{!! Form::text('email','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Your Email')) !!}
{!! Form::text('subject','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Your Subject')) !!}
{!! Form::textarea('msg','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Your Full Name')) !!}
{!! app('captcha')->display(); !!}
Copy after login

在浏览器中访问 http://laravelacademy.org/contact ,显示效果如下:

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template