grit / forms
Small form backend for static sites: CSRF, spam checks, MySQL storage, SMTP notifications and a scheduled spreadsheet export.
Requires
- php: >=8.4
- ext-mbstring: *
- ext-pdo: *
- ext-pdo_mysql: *
- openspout/openspout: ^5.0
- phpmailer/phpmailer: ^7.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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:
- A config file outside the web root. Copy
config.example.phpand fill it in. Don't commit it. Two endpoint files in the web root, e.g.
api/csrf.phpandapi/contact.php(seeexamples/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.phpThe form posting
FormDatawith the configured field names, the honeypot input andcsrf_token.examples/form.htmlhas 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:
- method is POST (405)
- CSRF token matches the session (403); the token is replaced after every POST
- the honeypot field is empty, and at least
spam.min_secondshave passed: otherwise it answers{"ok": true}without saving, so bots don't learn what tripped them - validation (400,
errors[]with all messages) - rate limit per IP (429)
- 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.
| Type | Stored as | Notes |
|---|---|---|
text | VARCHAR(max), default max 200 | optional pattern (regex) |
textarea | TEXT, default max 10000 | shown as a block in the email |
email | VARCHAR(max), default max 200 | validated, mailto: link in the email |
tel | VARCHAR(max), default max 30 | default pattern: digits, spaces, +()/.-, at least 6 |
checkbox | TINYINT(1) | required means it must be checked |
choice | VARCHAR | options; 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.