How-To PHP

How to stop spam from website contact form

Antispam Technic For PHP Custom Forms

Hello folks!!, In this article, we are going to explain how to avoid spam emails from the contact form. It is a simple method to get started. By adding this method to your all contact forms you can avoid spam emails.

Spam or Junk is the one which we wish it didn’t exist.  It’s annoying and serves no useful purpose.  Mails filled with junk emails, websites with bogus contact form submissions, and products hit hard by fake sign ups are only a few common victims of spam.  And unfortunately that’s here to stay.

Let’s get started!

To avoid this Spam we have to add an invisible field to your forms that only Spam – Bots can see, you can trick them into revealing that they are Spam in other words “Bots” and not actual end-users. This technique is called Honey Pot Technique.

<style type='text/css'> .first-name{ display: none; } </style>
<form action="Your-action-url.php" method="POST">
<label class="first-name">Name</label>
<input class="first-name" name="fname" type="text" />
<label>Name</label> <input name="name" type="text" />
<label>Email ID</label> <input name="name" type="text" />
<input name="submit" type="submit" value="Submit" />


In the above code, we have added a class name called “first-name” and we have hidden it through CSS. So the End-user or Customer of the website can’t able to see this field, Only spam will fill out this field. by using this technique we can differentiate BOTS and End-users. Use simple and common names as “email, phone, name, etc”. Now it comes to the validation part, In the data validation, we have add a condition like if the extra field has been filled the program should stop.
For Example :

if($_POST['fname'] != '')
echo 'This is spam';
//will terminate the program from here

Remember: This Anti-spam Technique For PHP Custom Forms is just a simple layer to prevent Spam – Bot attacks in a Simple and Easy way, some technologies can identify even these patterns, so use all the weapons you can against the BOTS and Spams. But i believe that, this is a simple Anti-spam Technique For the PHP Custom Forms pattern and it can avoid at least 50% of SPAM BOTS in your web page.

About the author


A coder who is trying to solve some problems via "Procoders", Software Engineer By Profession, WEB Enthusiast, Hobby Blogger. I love to share Tech Ideas and like to learn and explore new things and Technologies.

Add Comment

Click here to post a comment