How to Protect a Website Form from Spam Without a CAPTCHA
Put a request form on a website, and within a couple of weeks "requests" start arriving with casino ads, links to dubious websites and meaningless strings of letters. This is the work of bots that automatically find forms and fill them in by the thousands.
The first thing that comes to mind is adding a CAPTCHA. But a CAPTCHA annoys real clients, and some of them simply close the page. The good news: most bots are filtered out by checks that people don't even notice. Let's go through them with PHP examples.
How spam bots work
Most spam bots are simple: they load the page, find the form, fill in every field and submit it right away. Many don't run JavaScript and don't see the page styling — they work with the HTML code directly. The checks below rely on exactly these traits: a person behaves differently from a bot, and that can be detected.
A hidden honeypot field
You add a field to the form that a person doesn't see and doesn't fill in, while a bot fills it in like all the others. If this field has anything in it, the request was sent by a bot.
<div class="form-trap" aria-hidden="true">
<label for="website">Website</label>
<input type="text" id="website" name="website" tabindex="-1" autocomplete="off">
</div>
The field is hidden not with display: none — some bots skip such fields — but moved off the screen:
.form-trap {
position: absolute;
left: -9999px;
}
The tabindex="-1" attribute keeps the Tab key from reaching the field, autocomplete="off" stops the browser from filling it, and aria-hidden hides it from screen readers. On the server, the check takes one line:
if (($_POST['website'] ?? '') !== '') {
exit;
}
Here and below, exit is used for brevity. How best to respond to a bot without giving it hints is covered in a separate section below.
💡 Tip: give the field a plausible name — website, company, phone2. Smart bots easily recognize a field called trap or honeypot.
Checking the fill time
A person needs at least a few seconds to read the form and type a name and phone number. A bot submits the form almost instantly. So you add the time the form was shown, and the server checks how much time passed before it was submitted.
So that a bot can't just put in an old time, it's signed with a secret key. The key is any long random string stored only on the server:
<?php $formTime = time(); ?>
<input type="hidden" name="form_time" value="<?= $formTime ?>">
<input type="hidden" name="form_sign" value="<?= hash_hmac('sha256', (string)$formTime, FORM_SECRET) ?>">
When processing the form, the server checks the signature and the time:
$formTime = (int)($_POST['form_time'] ?? 0);
$expectedSign = hash_hmac('sha256', (string)$formTime, FORM_SECRET);
$elapsed = time() - $formTime;
if (!hash_equals($expectedSign, $_POST['form_sign'] ?? '') || $elapsed < 3 || $elapsed > 86400) {
exit;
}
This filters out requests sent in less than 3 seconds or more than a day after the page loaded, as well as requests with a forged time. The hash_equals function compares strings safely, so the signature can't be guessed from the response time.
⚠️ Important: if the whole page is cached, the time in the form will be the same for all visitors. In that case, it's better to insert the time with JavaScript or request it separately when the form opens.
Rate limiting
If a bot still gets past the checks, it can send hundreds of requests in a row. Limit the number of submissions from one IP address over a period of time. The easiest way is at the web server level — for example, in Nginx:
limit_req_zone $binary_remote_addr zone=forms:10m rate=5r/m;
server {
location = /send.php {
limit_req zone=forms burst=3 nodelay;
}
}
The first line goes in the http block of the configuration and creates a rule: no more than five requests per minute from one address. The location block applies it to the form handler. PHP handling must stay inside the same location block, just like in the main website configuration. If you don't have access to the server settings, a similar limit can be built in PHP itself by storing the times of recent submissions for each IP address in a file or database.
Server-side validation
Checks in the browser — required fields, email format — help people, but they don't stop bots: bots send data straight to the server. So all checks must be repeated on the server:
$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
$message = trim($_POST['message'] ?? '');
$isValid = $name !== ''
&& mb_strlen($name) <= 100
&& filter_var($email, FILTER_VALIDATE_EMAIL) !== false
&& mb_strlen($message) <= 3000
&& preg_match_all('~https?://~i', $message) <= 1;
The last condition filters out messages with more than one link — one of the most reliable signs of spam. If your clients don't need links in requests at all, you can ban them completely.
Security matters too: form data must never be inserted into an email or a page as is. Before output to HTML it's escaped with htmlspecialchars, and the sender's address must be validated before it's used in email headers, otherwise the form can be used to send emails on your behalf.
How to respond to a bot
If a request fails a check, don't show a detailed error like "you're a bot" or "too fast". That way the bot's author quickly learns which check to bypass. It's better to show the same success message as people see, but not pass the request on anywhere.
Errors that make sense to a person — a missing name, an invalid email — should be shown as usual, so a real client can fix them.
When you still need a CAPTCHA
The checks above stop almost all mass spam. A CAPTCHA may be needed if the form is targeted deliberately or if it can be used to cause harm — for example, to send SMS or emails to arbitrary numbers and addresses. In that case, choose invisible options: they check the visitor in the background and show a challenge only when the behavior looks suspicious.
Checklist
- the form has a hidden honeypot field with a plausible name;
- the server checks the form fill time and its signature;
- the number of submissions from one IP address is limited;
- all fields are validated on the server, not just in the browser;
- messages with several links are filtered out;
- form data is escaped before output and sending;
- a bot sees the usual success message, with no hints.
Summary
You can protect a form from spam without a CAPTCHA and without annoying clients. A hidden honeypot field and a fill-time check filter out most bots, rate limiting protects against mass attacks, and server-side validation catches what gets past the first filters.
🛡️ Is your website form flooded with spam? Message me — I'll set up protection so spam doesn't get through and clients don't notice a thing.
Published