|
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 MathGuardGo to the download section on this website and find there mathguard-v3.0.zip 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 MathGuardCurrent version is 2.0 Go to the download section on this website and find there mathguard-v0.2.zip 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
|
Comments
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 !
Regards,
Matej Koval
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'],
$_REQUEST['mathguard_code']))
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.");
}
break;
Thank you for this very useful piece of work. Feel free to share this on your site.
Mitch Vars
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 mathguard-v0.2.zip that you can download from codegravity.com.
Maybe the documentation could be a little better, but overall I would give this script 4/5.
message for Mathguard script
you can keep or remove the link, it's up to you.
But I think it would be nice to keep it there.
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.
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
Thanks!
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.
Are you planning to release a new version soon?
Which one to download? The v3.0? I am having problems finding the proper download file.
That clarifies things for me. Thanks so much for this wonderful spam protection add on!
If you'd any thoughts as to why and how to correct it, would be very grateful?
love it!
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.
thanks
Thank you in advance
great job!
can i use this class for my site?
Thanks for sharing.
RSS feed for comments to this post