Form spam needs layers: server validation, a honeypot, a throttle and a captcha verified on the server. Here is how hCaptcha fits a Laravel project and the production mistakes to avoid.
A contact form goes live and for a few weeks it works as intended. Then the inbox fills with casino links, fake enquiries and messages that exist only to test whether the form accepts anything. Someone on the team starts deleting them by hand, and a real enquiry gets lost among the noise.
We build forms like this into Laravel web applications all the time, and we also maintain an open-source hCaptcha package for Laravel. Below is how we think about stopping spam without turning every form into an obstacle course, including the mistakes that only show up in production.
Stopping form spam in Laravel: the short answer
Use layers. Validate every field on the server, add a hidden honeypot field for the laziest bots, put a throttle on the route, and add a captcha such as hCaptcha for the cases that get through. Verify the captcha token on the server, only on the request that actually saves or sends something, and test the whole thing with a fake verifier rather than the live service.
Why one measure is never enough
Form spam comes in several kinds, and each defence catches a different one.
- Server-side validation rejects malformed input, but a bot can send perfectly well-formed spam.
- A honeypot field is an input hidden from people with CSS. Simple scripts fill in every field they find, so a filled honeypot is a strong signal. It does nothing against a bot written for your specific form.
- Rate limiting caps how often one client can submit. It slows a flood, but it does not tell a person from a bot.
- A captcha asks the visitor, or the browser, to prove something a simple script cannot. It is the layer that separates people from automation, at the cost of some friction.
Start with the cheap layers. Many sites never need more than validation, a honeypot and a throttle. Add a captcha when the spam keeps coming, or when the form triggers something expensive: an email to a salesperson, a record in a CRM, a message to a third-party service.

Where hCaptcha fits
hCaptcha is one of several captcha services, alongside reCAPTCHA and Cloudflare Turnstile. The way it works is the same in all of them. The widget in the browser produces a token when the visitor passes the challenge. Your form sends that token with the other fields. Your server then sends the token and your secret key to the provider and gets back a verdict.
The important part is the last step. A token that was only checked in the browser proves nothing, because a bot can skip the browser. The verification must happen on your server, and the secret key must never be sent to the page.
hCaptcha is a third-party service that loads a script and talks to its own servers. That has a privacy side, so decide how it fits your privacy notice and your consent setup. We cover the consent side in our article on cookie consent in Laravel.
The mistakes that appear only in production
Adding a captcha looks like a ten-minute job: put the widget on the page, post the token to the verification endpoint, check the result. That version works on your machine and then fails in ways that are hard to see, usually as a visitor who cannot submit and does not know why.
The token can only be used once
hCaptcha tokens are single-use. If one form is guarded by both a validation rule and a route middleware, and each one verifies the token on its own, the second check is told that the response was already seen. The visitor solved the captcha correctly and still gets an error. The fix is to verify each token once per request and reuse the verdict everywhere else. That is the main reason we wrote a package instead of repeating the same snippet in every project.
The sitekey is public
The sitekey sits in your HTML, so anyone can copy it. A person can embed your sitekey on a page they control, solve the challenge there and post the genuine token to your form. The provider will confirm that the token is real, because it is. Two protections help here. In the hCaptcha dashboard, restrict the sitekey to your own domains. On your server, compare the hostname the provider reports with the hostnames you expect, and keep that check switched on. Treat the hostname check as one useful layer, not as proof of origin.
The secret key stays on the server
The secret key belongs in your environment configuration, not in a Blade view and not in JavaScript. If it is missing, the right behaviour is a loud error that you notice during deployment. The wrong behaviour is silently rejecting every visitor.
Do not add the required rule
This one surprises people. With our package, the captcha rule already knows how to report a missing token with a clear message. If you also write the required rule next to it, Laravel's generic message wins, and the visitor sees a vague error instead of a request to complete the captcha.
Where each piece goes in a Laravel project
| Layer | Where it lives | What it protects against |
|---|---|---|
| Validation | Form request or controller | Malformed or missing data |
| Honeypot | Hidden field plus a validation check | Simple scripts that fill every field |
| Throttle middleware | On the state-changing route | Floods from one client |
| Captcha rule or middleware | On the same route, or in validation | Automated submissions in general |
Two placement rules save a lot of grief.
- Put the captcha check on the POST route only. A middleware that verifies a token on every request it sees will also run on the GET that shows the form, where there is no token yet, and the page itself fails. Never put it on a route group that also serves the form.
- Pair it with throttling. A captcha verifies a token. It does not limit how often someone submits. Our package does not rate limit, and a captcha check on its own never will, so add Laravel's throttle middleware to the same route.
If your form runs in Livewire, use the validation rule alone rather than stacking the middleware on the Livewire route, and make sure the widget is rendered again after the page updates. Also remember that a captcha that guards a Livewire action does not promise that one solved challenge means exactly one submission, so keep the throttle in place there too.
Decide what happens when hCaptcha is down
Every external check has an outage policy, whether you chose one or not. There are two honest options.
- Fail closed. If the provider cannot be reached, the submission is rejected. You lose genuine enquiries during an outage, but no spam gets through.
- Fail open. If the provider cannot be reached, the submission is accepted. You keep the enquiries, and for that window the captcha is not protecting you.
For most business sites we prefer failing closed on forms that trigger expensive actions, and we make sure the outage is logged so someone notices. For a low-risk form where a lost message costs more than a spam message, failing open can be a reasonable choice. Make it a decision rather than an accident.
Test it without calling the live service
A test suite that depends on a captcha provider is slow and fragile, and it should not need a real solved challenge. Replace the verifier in tests. Our package ships a fake that accepts tokens and lets you assert that verification happened for the right field and the right number of times. If you do not use a package, fake Laravel's HTTP client instead and assert that exactly one request went out for one submission.
Test the failure paths too: a missing token, a token the provider rejects, and the case where the same token reaches two guards. Those are the situations your visitors will find first.
hCaptcha publishes public test keys that always verify. They are useful for local development and automated tests. They must never reach production, because a form that always passes protects nothing. Check which keys a deployment is using before you ship.
A short checklist before you ship
- The sitekey is restricted to your domains in the hCaptcha dashboard.
- The secret key is in the environment configuration, not in the page or the repository.
- The hostname check is on and the list of allowed hostnames is correct for every host that serves the form.
- The captcha check sits on the POST route only, never on the page that shows the form.
- A throttle sits on the same route.
- The rule is not combined with the required rule.
- You decided whether a provider outage fails open or closed, and it is logged.
- Tests use a fake verifier, and production does not use the public test keys.
- The privacy notice and consent setup mention the captcha service.
Do it once, in a package
Every item above is easy on its own. The trouble is repeating them correctly across several sites and forms, and having them survive the next developer. That is why we keep the logic in one place. You can read what our package does and how it is configured on its project page.
If the form belongs to a larger product, our article on why Laravel suits business applications explains why we build them this way.
If you need forms like this built into a larger system, see our web application development. You can also contact us and describe what is arriving in your inbox.