MathGuard PHP form anti-spam protection

If you are looking for the MathGuard anti-spam patch for AkoComment TSE 1.4.6, please see: MathGuard anti-spam patch for Joomla's Akocomment component


Creating this website made with PHP, I attracted lots of spambots to flood my forum, my programming resources, freelance database and other forms, but you probably know this situation very well.  So I decided to create a simple anti-spam PHP Class which could help me and probably other people to deal with the spam.

MathGuard is free PHP class that everyone can use as anti-spam protection for the website forms. It's also a suitable solution when there is no PHP GD support from the webhosting company.

UPDATE: MathGuard version 3.0


Here's an example how to use MathGuard to protect your forms  (Adobe Flash player required)



New version features a better way of rendering the expression that user has to evaluate. The numbers consist of 3x5 random character matrix.

featuring more complex Anti-spam security question rendering - these numbers consist of the random letters

Download UPDATED version MathGuard

Go to the download section on this website and find there

Installation process is the same as in the version 2.0, so please install it the way it is described below.


The older version - MathGuard 2.0

You can still install the older version:


You have probably seen this solution before on some other websites, it's nothing new,
but I did it by myself and the way I wanted and I am using this anti-spam everywhere it's needed. The good thing is, that it works.


The principle of this PHP anti-spam class

The principle of this PHP anti-spam class is very simple - The class inserts a small piece of HTML code into your PHP form -
an expression consisting of two random numbers, one text input field for user's answer,
and one hidden field with the hashcode.

When user submits the form with the answer, the answer is being hashed and compared to
the security code that has been submitted as well.


How to set up this PHP anti-spam class and use it on your website?

1. Download the code of MathGuard

Current version is 2.0

Go to the download section on this website and find there

2. Unzip the archive

and copy the file ClassMathGuard.php into the folder with forms and action handlers you want to use.

3. Open the code with the PHP or HTML form you want to protect


4. Add the following PHP code snippet (the one in the ellipse)

5. Protect the form handler

		/* first we need to require our MathGuard class */
require ("ClassMathGuard.php");
/* this condition checks the user input. Don't change the condition,
just the body within the curly braces */
if (MathGuard :: checkResult($_REQUEST['mathguard_answer'],
$_REQUEST['mathguard_code'])) {
echo ("Great !");
//insert your code that will be executed when user enters the correct answer
} else {
echo ("Bad answer, go back to school !");
//insert your code which tells the user he is spamming your website

6. Open the form in your browser and you should see the mathguard's anti-spam security question there

7. Example - watch the flash demo above

There is also an example in the downloaded archive. It features a simple form and one simple form
handler which displays the data. I recommend you also to watch the flash demo on this website that shows a real example how to integrate mathguard.


8. Enjoy !

If you'd have any questions regarding my PHP anti-spam class, feel free to write to the comments or directly to my email



-3 #1 Svetyshka 2012-04-26 08:56
Useful info, guys. Thanks!
-2 #2 Jessica covarrubias 2013-08-29 23:18
I'm filling out an app online they have the security question 3+2=4 it keeps telling me it has the wrong answer and not letting me submit my application it's fuckin annoying wtf?

Add comment

Security code

This website uses cookies to ensure you get the best experience on our website. Learn More.

Got It!