项目作者: isometriks

项目描述 :
Symfony3 Form Spam Protection
高级语言: PHP
项目地址: git://github.com/isometriks/IsometriksSpamBundle.git
创建时间: 2013-07-10T21:39:16Z
项目社区:https://github.com/isometriks/IsometriksSpamBundle

开源协议:MIT License

下载


Symfony SpamBundle

Please feel free to send pull requests. I would like to incorporate a bunch of
spam methods into this project.

Installation

Install via Composer:

  1. $ composer require isometriks/spam-bundle

If you’re not using Symfony Flex, add the bundle to your config/bundles.php file:

  1. // config/bundles.php
  2. return [
  3. // ...
  4. Isometriks\Bundle\SpamBundle\IsometriksSpamBundle::class => ['all' => true],
  5. ];

Currently we have:

Timed Spam Prevention

Requires forms to be sent after a certain amount of time. Most bots won’t wait
to submit your forms, so requiring an amount of time between render and submit
can help deter these bots.

A side affect of this spam prevention is that you won’t be able to refresh
a page to resubmit data UNLESS the view is rendered again $form->createView()
This is because the event listener removes the start time of the form and
when it can’t find it, will cause the form to be invalid. You could set your
min time to 0 to just make use of this feature

Also note that this spam protection will also apply this limit to forms that
are not filled in correctly and need to be resubmitted. A high minimum time
could affect those users who only need to fix one field quickly

Configuration:

  1. # config/packages/isometriks_spam.yaml
  2. # Copying this config is not necessary. These are defaults, only copy
  3. # what you'd like to change.
  4. isometriks_spam:
  5. timed:
  6. min: 7 # seconds
  7. max: 3600
  8. global: false
  9. # message also takes translator strings.
  10. message: You're doing that too quickly.

Usage:

  1. $this->createForm(MyType:class, null, [
  2. 'timed_spam' => true, // Just this line is required to enable this feature, the rest is to override settings
  3. 'timed_spam_min' => 3,
  4. 'timed_spam_max' => 40,
  5. 'timed_spam_message' => 'Please wait 3 seconds before submitting',
  6. ]);

Or

  1. public function configureOptions(OptionsResolver $resolver)
  2. {
  3. $resolver->setDefaults([
  4. 'timed_spam' => true,
  5. // ...
  6. ]);
  7. }

Honeypot Spam Prevention

A honeypot is a way to trick bots into filling out a field that should not
be filled out. It is hidden and can be named something usual so that any
bots / crawlers will think it is a real field.

If the field is filled out, then the form is invalid. You can optionally
choose to use a class name to hide the form element as well in case the
bot tries to check the style attribute.

  1. # Copying this config is not necessary. These are defaults, only copy
  2. # what you'd like to change.
  3. isometriks_spam:
  4. honeypot:
  5. field: email_address
  6. use_class: false
  7. hide_class: hidden
  8. global: false
  9. message: Form fields are invalid

Usage:

  1. // Only honeypot = true is required to enable, the rest are to override settings
  2. $this->createForm(MyType::class, null, [
  3. 'honeypot' => true,
  4. 'honeypot_field' => 'email_address',
  5. 'honeypot_use_class' => false,
  6. 'honeypot_hide_class' => 'hidden',
  7. 'honeypot_message' => 'Form fields are invalid',
  8. ]);

Or

  1. public function configureOptions(OptionsResolver $resolver)
  2. {
  3. $resolver->setDefaults([
  4. 'honeypot' => true,
  5. // ...
  6. ]);
  7. }

Twig Form Error Message Rendering

Form errors come from the form itself, so if you want to display the errors
you’ll need to make sure this is in your template.

  1. {% if form.vars.errors is not empty %}
  2. {{ form_errors(form) }}
  3. {% endif %}