How-To PHP

Antispam technique for forms

Antispam Technic For PHP Custom Forms

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.
To avoid this Spam we have to add a invisible field to your forms that only Spam – Bots can see, you can trick them into revealing that they are Spam – Bots and not actual end-users. This technique is called Honey Pot Technique.

<style type='text/css'>
display: none;
<div >
<form action='Your-action-url.php' method='POST'>
<label class='first-name'>Name</label>
<input type='text' class='first-name' name='fname'>
<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 i have added a class name called “first-name” and i have hided 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. Use simple and common names as “email, phone, name, etc”. Now the validation part, Write a condition while inserting the form values.
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 this patterns, so use all the weapons you can against it. But i believe that this is a simple Anti spam Technique For PHP Custom Forms pattern and it can avoid at least 50% of spams in your web page.

About the author



Vishwas Hegde is the architect of I'm a Software Engineer and technology enthusiast person who loves to learn and explore new things and technologies. When i'm not writing content for my websites, I love to travel and explore the nature.

Add Comment

Click here to post a comment