DEV Community

Allan Mwaura
Allan Mwaura

Posted on

4

How to prevent web forms from spam

In the world of today,as a website/blog owner,you tend to receive tons of emails/comments from bots.Different prevention techniques have been taken,this article aims will cover honeypot method.

The honeypot method uses a hidden field on forms,as a trap for "less intelligent" bots who will automatically fill the form.

<form action="mail.php" method="POST">
<p>
    <label>Name</label>
    <input type="text" name="first_name">
</p>
<p>
    <label>Email</label>
    <input type="email" name="email">
</p>
<p id="phone">
    <label>Tel</label>
    <input type="text" name="phone_number">
</p>
<p>
    <label>Comment</label>
    <textarea name="your_comment"></textarea>
</p>
<p>
    <button type="submit">Submit</button>
</p>
Enter fullscreen mode Exit fullscreen mode

We then hide the field using CSS,so that visitors(assuming they are human) won't see it.On your CSS file add the following line

#phone{display: none;}

So here is where the catch is,any submission with not empty phone field,is spam.

if(!empty($_POST['phone_number']))
die();

Thanks for reading!!.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more