DEV Community

Allan Mwaura
Allan Mwaura

Posted on

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!!.

Top comments (0)