Search by

grit / forms

gresakg

Small form backend for static sites: CSRF, spam checks, MySQL storage, SMTP notifications and a scheduled spreadsheet export.

v1.0.2 2026-11-30 12:17 UTC

This package is auto-updated.

Last update: 2026-11-30 11:18:23 UTC


README

A small form backend for static sites (Eleventy, plain HTML, …) on a PHP host:

  • CSRF token, honeypot, minimum fill-in time and a per-IP rate limit
  • validation from a field list in the site config
  • submissions stored in MySQL/MariaDB (the table is created on first use)
  • SMTP notification via PHPMailer, with Reply-To set to the visitor
  • grit-form-export: submissions as an .xlsx file, to a file or by email from cron

No admin UI and nothing to log into: the site owner gets submissions by email and, on a schedule, as a spreadsheet.

Requires PHP 8.4+ with pdo_mysql, mbstring and zip.

Install

composer require grit/forms

The site needs three things:

  1. A config file outside the web root. Copy config.example.php and fill it in. Don't commit it.
  2. Two endpoint files in the web root, e.g. api/csrf.php and api/contact.php (see examples/api/). Each is one line after the autoloader:

    Grit\Forms\Endpoint::csrf(require __DIR__ . '/../../site-config.php');   // csrf.php
    Grit\Forms\Endpoint::submit(require __DIR__ . '/../../site-config.php'); // contact.php
    
  3. The form posting FormData with the configured field names, the honeypot input and csrf_token. examples/form.html has a minimal form and script.

Keeping the form definition in the site repo

The field names have to match the form markup, so it can be handier to keep the form definition in the site's repository and only the credentials outside the web root. The config file just has to return the merged array:

// api/form-config.php (in the repo)
$credentials = require __DIR__ . '/../../site-credentials.php'; // db_*, smtp_*, mail_to
return $credentials + [
    'fields' => [ /* ... */ ],
    'spam'   => [ /* ... */ ],
];

Point both endpoints and grit-form-export --config= at that file. It returns an array and prints nothing, so it's harmless if requested over the web.

Deny web access to vendor/ if it sits inside the web root (nginx: location ^~ /vendor/ { deny all; }).

How a submission is handled

GET csrf.php starts the session, issues a token and, if the timer isn't already running, records the time. A new token after an error or from a second tab doesn't restart the timer; a successful save clears it. POST contact.php then checks, in order:

  1. method is POST (405)
  2. CSRF token matches the session (403); the token is replaced after every POST
  3. the honeypot field is empty, and at least spam.min_seconds have passed: otherwise it answers {"ok": true} without saving, so bots don't learn what tripped them
  4. validation (400, errors[] with all messages)
  5. rate limit per IP (429)
  6. save, then send the notification. If only the email fails, the visitor still gets ok (the submission is saved) and the error goes to the PHP error log.

Fields

Defined under fields, in display order. The same list drives validation, the table, the notification email and the export columns.

TypeStored asNotes
textVARCHAR(max), default max 200optional pattern (regex)
textareaTEXT, default max 10000shown as a block in the email
emailVARCHAR(max), default max 200validated, mailto: link in the email
telVARCHAR(max), default max 30default pattern: digits, spaces, +()/.-, at least 6
checkboxTINYINT(1)required means it must be checked
choiceVARCHARoptions; an invalid value falls back to default if set

Every field takes label, required, max and messages (per-field overrides, e.g. 'messages' => ['required_checkbox' => 'Please accept the terms.']).

The table is only created, never altered. If you add a field later, add the column yourself (ALTER TABLE …).

Texts and languages

All visitor-facing and email texts are in messages (English by default) and in the field labels, so a site can be fully in another language. The email subject can depend on a language field: 'lang_field' => 'lang' and 'mail_subject' => ['en' => '…', 'sl' => '…'].

Export

# into a file, for yourself
vendor/bin/grit-form-export --config=/var/www/example.com/site-config.php --since=2026-01-01 --output=forms.xlsx
vendor/bin/grit-form-export --config=… --all > forms.xlsx

# by email to export.mail_to (or mail_to): only rows not exported before
vendor/bin/grit-form-export --config=… --mail

--mail sends nothing when there are no new rows and remembers the last exported row in the grit_forms_export table. With --all or --since it sends that selection without touching what's remembered. It prints nothing on success; errors go to stderr with exit code 1, so cron's mail or your monitoring sees them.

A cron line for a monthly export:

0 7 1 * * php /var/www/example.com/htdocs/vendor/bin/grit-form-export --config=/var/www/example.com/site-config.php --mail

Every value is written as a text cell, so a submitted =… never becomes a formula. The visitor's IP is left out unless export.include_ip is true.

Privacy

Submissions are personal data. The package stores only the configured fields, the IP (for the rate limit) and the time. Deleting old rows is up to the site owner; a retention job isn't included.

Testing

GRIT_FORMS_TEST_DSN='mysql:host=localhost;dbname=grit_forms_test;charset=utf8mb4' \
GRIT_FORMS_TEST_USER=… GRIT_FORMS_TEST_PASS=… composer test

Uses a scratch database; it creates and drops its own tables.

Support

This is maintained for my own clients' sites and shared as is. Issues and merge requests are welcome, but there's no promise of support. Security problems: see SECURITY.md.

Licence

GPL-2.0-or-later. See LICENSE.