Create an account | Log in | Password | Contact Contact us

Get ExtraWatch PRO:

Special discount
9,99 EUR lifetime license
Money back guarantee

Extension directory rating:
rating 4.86 rating 4.86 rating 4.86 rating 4.86 rating 4.86


ExtraWatch is web analytics website visitor tracking tool. Learn more...

Features: Click heatmap, conversion tracking, interactive HTML5 traffic flow chart and many other useful functionalities, which will help you to optimize your site.


Freelance profiles Freelance
Open freelance projects Projects
Joomla and JoomlaWatch forum Forum
Best programming websites Resources
Best programming websites  Site map

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



0 #1 Mike 2007-05-03 16:57
This site displays very poorly in Firefox, I'm sorry to say.
-1 #2 matto 2007-05-03 17:06
Could you please specify what's wrong that I can make the corrections? Are there also some other people experiencing the same problems? Thank you
+2 #3 Mitch 2007-05-09 22:54
I'm having a lot of trouble with spam from the contact form on one of my Joomla sites. MathGuard looks like a good solution. I have been trying to install it but I can't get it to work. Where does the form handler code go? I'm not sure which page handles the form.
-1 #4 Matej Koval 2007-05-09 23:38
Hello Mitch,

First you have to find out what code handles your form on submit.
It's usually the one which contains the SQL insert command or some other action.
(eg. with the joomlaboard forum component it's the /components/com_joomlaboard /post.php
line 186).

When you find this piece of code, you have to wrap it in one IF block with the mathguard condition:

some code
// we need to tell PHP to require our class

require ("ClassMathGuard.php");

if (MathGuard :: checkResult($_R EQUEST['mathguard_answe r'],
$_REQUEST['mathguard_code'] ) ) {

// original code with the
database insert wrapped by the IF condition

} else {
// code that handles the wrong answer
some code

I hope I explained it well. If you'll have some questions, feel free to ask !


Matej Koval
+4 #5 Mitch Vars 2007-05-16 22:26
I managed to get this to work by putting the IF condition in the switch on line 30 of contact.php - it should look something like this:

switch( $op ) {
case 'sendmail':
/* 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($_R EQUEST['mathguard_answe r'],
sendmail( $con_id, $option );
} else {
//insert your code which tells the user he is spamming your website
echo ("Sorry - Your email has not been sent. Please help us to combat spam by correctly answering the math problem before sending your email.");


Thank you for this very useful piece of work. Feel free to share this on your site.

Mitch Vars
+3 #6 Petar 2007-05-19 18:33
I've just downloaded and installed MathGuard example on my web server and every time I try to submit the form it tels me the phrase "Bad answer, go back to school". I don't know what's wrong since it is the authors example? Please, help! It looks fine antispam method protection for me.
+1 #7 Matej Koval 2007-05-21 15:59
Hello Petar,
I'm sorry, there was an error in the example code, you were right.
In the formhandler.php, there has to be $_REQUEST instead of $request.

The error is now corrected and it's bundled in the that you can download from
+1 #8 Rob Hope 2007-05-27 11:46
Thanks for a great spam filter! Took roughly 45 minutes to code the script into my contact form and css all the styles etc to make it look part of the website.

Maybe the documentation could be a little better, but overall I would give this script 4/5.
-1 #9 kumz 2007-05-30 13:51
hei, great job! can i use this class for my site without referencing to this site?
+1 #10 TITA STEFAN 2007-05-30 14:03
On the download section I receive a "forbidden access"
message for Mathguard script
-1 #11 matto3c 2007-05-30 14:23
you can keep or remove the link, it's up to you.
But I think it would be nice to keep it there.
-3 #12 Peter 2007-06-20 01:45
Just wondering wich steps to take to make it work in joomla version 1.0.12
+2 #13 Philip 2007-07-12 17:59
Will MathGuard work with a form that was created and published with Frontpage?
-1 #14 Matej Koval 2007-07-12 19:06
MathGuard needs PHP to work, so you can't just include it into a plain HTML with no PHP support.

You can do so, if the .html extension is interpreted by PHP and the piece of code that mathguard generates is inserted there.
The form handler has to be also a PHP-interpreted page that uses MathGuard's function to check the right answer.

I hope I explained it well, if you'll have any further questions, feel free to ask me.
-2 #15 Djay 2007-07-24 15:48
Thanks for this script really easy to use and I hope efficient ! Well, I'll tell you in few days... :-)
-1 #16 Cambo 2007-07-29 04:13
It took me about 5 minutes to incorporate mathguard into my existing contact form. It worked straight out of the box. Very impressed. I give it 4/5. It would rate a 5 but it doesn't display well in Opera. Thanks for a very simple way to protect from spammers.
+2 #17 Luca 2007-08-08 04:50
I'm using it on my site that has more than one form and I must say it seems to work pretty well. I wanna thank you for the script.
I also moved the the link to your site to a less visible place without taking it away. A person can still find it easily.

I hope it's fine for you :-)
+1 #18 FlorinP 2007-08-14 17:47
It really works! I set it up in a snap, fitted in perfectly :-)
-1 #19 Winnie 2007-08-16 20:51
Hi, I really hope mathguard can help me with those spammers. Where to paste the code in post.php? I'm not a good programmer. Shall I paste the whole block of If-then-else into 1 block or have to find the code that insert the database and then break your if-then-else apart? Can anyone help me to paste the snippet into joomlaboard/post.php? Thanks very much in advance.
+1 #20 norton 2007-08-20 17:31
Mathguard didn't display well for me in Firefox. I did some investigating, using the CSS with no luck. Finally I checked my Firefox font display settings:
Tools > Options... > Content > Font & Colours > Advanced > Minimum font size - set it to "none".
Reload page.
Mathguard now displays well.

This is a per user fix, obviously, and I have no idea how to ensure that my site visitors have their Minimum font size set to "none".

Hope this helps.
+1 #21 The Pearls Master 2007-09-25 12:42
Thank you very much for this simple and powerful class! I incorporated MathGuard in my site in a few minutes.

Are you planning to release a new version soon?
+2 #22 Dragon 2007-11-08 15:46
Nice spam protection and very easy to implement. If I can suggest one more option to implement with the random image because many people get use to it. Anyway the counting filter is very effective.
-1 #23 matto 2007-11-08 15:49
I'm working on the new improved version. So if you have some more suggestions, please post them here.
0 #24 matto 2008-02-05 16:04
Yes, you are unfortunatelly right. Please re-download and re-install the .zip archive. The error has been corrected.
0 #25 matto 2008-02-05 16:05
... Or, you can just replace the ClassMathGuard.php that comes in this new archive in your root Joomla directory.
+1 #26 Gman 2008-02-05 16:15
Thats great! I didn't realize I was running an old version, my apologies.
-1 #27 Gman 2008-02-05 16:18
Hi Matto,

Which one to download? The v3.0? I am having problems finding the proper download file.
0 #28 Gman 2008-02-05 16:29
Hi Matto,

That clarifies things for me. Thanks so much for this wonderful spam protection add on!
+1 #29 Joe 2008-07-11 02:35
You could save everyone alot of time by just doing that. Also, what files are we supposed to edit in steps 4 & 5? It would take you 5 minutes to update this info and save your customers hours.
-1 #30 matto 2008-07-11 09:47
It's done now.
+1 #31 iskandar 2008-08-10 18:16
The best antispam protection. keep up the good work
0 #32 Matti 2008-08-29 20:16
For some reason I can't get the characters in the Mathguard question for Akocomment to display in red, or in a small-sized font. I've checked the php file but the parameters are correct: they just don't display on the page.

If you'd any thoughts as to why and how to correct it, would be very grateful?
+1 #33 Renan 2008-08-31 07:16
This was the best antispam protection I ever seen. I will try it and if this is really good, i will spread the words to everyone.

love it!
+1 #34 Naeem 2008-09-02 07:58
Great article for php web programming
0 #35 iva 2008-10-09 18:35
i've been using this code for several months and till today everything was ok, but i just received email snet from the form on the site with spam massege inside of the body.

can robots read it? and is there a session limit, because i heerd that robot can like with captcha to use the same code for many submits.

i will give you more info if you need.

+1 #36 hotchilidamo 2009-01-27 18:38
My forms use Javascript validation, is there any way to impliment Math Guard validation in Javascript?
+4 #37 Frodon 2009-03-12 10:18
Thanks a lot for this class. Works well !
-1 #38 PC NET WORKS 2009-03-16 20:24
It looks very elegant and everybody would love to have it in Joomla1.5 contact form. How can this happen? Can somebody write what code, where (exact files e.g. /components/contact/xxxx.php)?
Thank you in advance
+3 #39 Dominique 2009-08-10 03:47
Hi I a trying this out, the version 3.. But I a a bit dense... I am not sure where to put what... My form is an HTML email form handled by PHP.
0 #40 Laptop Battery 2009-11-23 02:56
-1 #41 Martin Lawrence 2010-02-12 14:51
great job!
can i use this class for my site?
Thanks for sharing.
+1 #42 Svetyshka 2012-04-26 08:56
Useful info, guys. Thanks!
0 #43 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

Recommended: featured at:

Powered by Website Informer

We have now:
we have now pagerank 8 !

Web analytics, Heatmap

Freelance ColdFusion, Flex, PHP

Obraz Bratislava

WinAsm Studio

Vyšné Ružbachy

Sochy, Reštaurovanie


© Copyright 2003-2014 - powered by ExtraWatch visitor tracking and heatmap tool