Guides › HTML / PHP sites › Security & hacks

Stop Spam From Your Contact Form (HTML, PHP and WordPress)

Bots fill your form with junk. Add a hidden field, server-side checks, rate limits and a CAPTCHA if needed.

Reference guide. This guide is written from established practice and the official documentation of the tools involved. We have not yet tested every step on a live demo site. Back up your site before you change anything.
Security and hacking

Spam bots find forms automatically. A few layers stop most of them without annoying real visitors.

Layer 1: A honeypot field

Add a field that people cannot see but bots fill in. If it has a value, ignore the submission.

<div style="position:absolute;left:-9999px" aria-hidden="true">
  <label>Leave this empty <input type="text" name="website" tabindex="-1" autocomplete="off"></label>
</div>

On the server, in PHP:

if (!empty($_POST['website'])) { http_response_code(200); exit; }

Layer 2: Check on the server

Never trust the browser alone. In PHP, validate the email with filter_var($email, FILTER_VALIDATE_EMAIL), limit message length, and reject messages with many links.

Layer 3: Slow the bots

Record when the form was loaded and reject submissions made in under 2 to 3 seconds. Limit how many messages one address can send in a minute.

Layer 4: A CAPTCHA if the spam continues

Services such as Google reCAPTCHA, hCaptcha and Cloudflare Turnstile verify the visitor. Add the widget to the form and verify the token on the server, since a check done only in the browser is useless. Make sure to mention the service in your privacy policy.

Layer 5: Protect the mailbox

Send the message by authenticated SMTP, not mail(). See our PHP form guide. Do not put raw visitor input into email headers.

On WordPress

Use the spam options of your form plugin, and an anti-spam plugin. See our spam comment guide.

Related guides

Did this solve it? If not, email hello@sitefixlab.org with the exact error text and we will look into it. Try the problem finder.

Last updated 2026-11-21.