Finding lasting solution to spamming in form

Finding lasting solution to spamming in form

What is a spam bot?

A spam bot is a malicious program or software designed and used by soammers to to submit forms with fake and real email addresses, attaching links to their private web pages. Spam bot can cause series of problems.

Some simply spam your comment section in a form for the purpose of SEO backlinks which allow them to gain authority.

One thing is for sure, spammers are never relenting once they target your websites, they just keep spamming your contents and posting gibberish which are not even related to the content, some can send malicious email via your contact forms and this is very annoying for most developers managing a website.

There is a big problem with the spammers nowadays. There are a lot of scripts that are running throughout different websites and try to submit a form wherever they come across.

There goes the question: what is the lasting solution to prevent spamming on your website.

The solution varies depending on your website type or frameworks you are using: Laravel framework, WordPress,  PHP, and any other frameworks you are using.

I’ll be proffering solution using php which works on any php framework or web pages with PHP codes.


  1. Re-captcha
  2. Honeypots
  3. Plugins and packages etc.

The simple way of doing this is  as follows:

Write a php code declaring the following values

$min = 1;
$max = 15; // any max you want, but keep it simple, so you don’t leave your users to be solving complex math.
$dig1 = rand($min + $max);
$dig2 = rand($min + $max);
These gives two random numbers
$sum = $dig1 + $dig2;

So in your form you would have and hidden input field with the value of $sum

Label: what is: $dig1 .’ + '. $dig2

<input type=”text” name=”quiz” placeholder=”write your answer here”>
<input type=”hidden” style=”display:none” name=”quizAns” value=”$sum” readonly>

Note that this code should be on the page you are processing the form, so that the value can be sent together with the rest of the input to your controller or processing page.

If you are using Laravel you can simply use 

@php declare variable here @endphp.

After this you can then use an if else statement to either reject the form when answer is wrong or accept it when it’s right.

This solution would stop the spam bot, but need to add validation to ensure that even when the spammers input a link or tags, it’s validated.

Your contents assigned value e.g $content we can then use this to validate

$content = Strip_tags($content); // this remove any html tags.

Then you can declare a regular expressions to replace every link with a white space, and afterwards save it to your database.

With this, we have been able to sanitize our content, so that we don’t see unnecessary links in our form request.

People have used Akismet extensively on WordPress website, but it can also be used with any app including Laravel. You’ll just need to tap into their API or use an existing page like nickurt/laravel-akismet.

Stop Forum Spam is a free service that sounds similar to Akismet and Laravel packages like nickurt/laravel-stopforumspam exist to help with the integration.

That’s it guys, I hope this is helpful, you can drop a comment if you need any clarifications and  it would be answered accordingly.

Enjoy this article ? drop a comment below and add your own contribution.

Maye Jacob

Jacob Olorunmaye

Verified Expert
Passionate Backend Developer | Expert in PHP, Laravel, JavaScript, and Golang 🚀 | Crafting seamless, scalable solutions for the digital era

Leave a Reply

Your email address will not be published. Required fields are marked *