用于Laravel框架的 No CAPTCHA reCAPTCHA 实现

jopen 7年前

recaptcha_anchor 2x

Installation

Add the following line to the require section of composer.json:

{      "require": {          "anhskohbo/no-captcha": "*"      }  }

Run composer update.

Laravel

Setup

Add ServiceProvider to the providers array in app/config/app.php.

'Anhskohbo\NoCaptcha\NoCaptchaServiceProvider',

Configuration

Run php artisan config:publish anhskohbo/no-captcha (publish:config if you use Laravel 5).

Fill secret and sitekey config in app/config/packages/anhskohbo/no-captcha/config.php file:

<?php    return array(        'secret'  => '',      'sitekey' => '',    );

Usage

Display reCAPTCHA

Insert reCAPTCHA inside your form using one of this examples:

For Laravel 4

<?php echo Form::captcha() ?>

For Laravel 4 using Blade syntax

{{ Form::captcha() }}

For Laravel 5

<?php echo app('captcha')->display(); ?>

For Laravel 5 using Blade syntax

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

For Laravel 5 with illuminate/html package using Blade syntax

{!! Form::captcha() !!}
Validation

Add 'g-recaptcha-response' => 'required|captcha' to rules array.

$validate = Validator::make(Input::all(), [      'g-recaptcha-response' => 'required|captcha'  ]);

Without Laravel

Checkout example below:

<?php    require_once "vendor/autoload.php";    $secret  = '';  $sitekey = '';  $captcha = new \Anhskohbo\NoCaptcha\NoCaptcha($secret, $sitekey);    if ( ! empty($_POST)) {      var_dump($captcha->verifyResponse($_POST['g-recaptcha-response']));      exit();  }    ?>    <form action="?" method="POST">      <?php echo $captcha->display(); ?>      <button type="submit">Submit</submit>  </form>

项目主页:http://www.open-open.com/lib/view/home/1418009201277