Click here to Skip to main content
14,693,599 members
Articles » Web Development » Web Security » General
Posted 11 Jan 2013


3 bookmarked

MathGuard: A Very Simple Alternative to Regular CAPTCHA for Beginners

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
12 Jan 2013CPOL
MathGuard is a security option for beginners, developed by PHP and jQuery


MathGuard is a very simple alternative to image-based CAPTCHA to ensure the fight against brute-force attack on you PHP applications. I have developed a small but nice class to put it into action. The specialty of this MathGuard is not only Addition is functioning here; I also have set Subtraction and Multiplication together.

Source Files

  1. index.php – the landing page, where I keep the HTML form
  2. class.mathguard.php – the file containing class
  3. jquery.js – the jQuery file
  4. style.css – the CSS file


As I said earlier, the index.php is the only landing page here where I kept the form to show what result system demands from users. It’s usually as follows:

Image 1

Considering the numeric values and mathematical symbol (+, -, x) in between those of the green box, the text box contained in red box, demand the actual result to continue through the application.

After action taken by submit button, a jQuery based function takes decision about the given result, either it's right or wrong, and shows a display as follows:

Image 2


Image 3

Using the Code

On index.php file, you will find that a class has been included as:

$objMG = new MathGuard; 

And the object objMG called three functions as:

numbers_pool(lower_limit, upper_limit) 

which takes two parameters as a limit of sample numbers from where I have picked two numbers randomly to calculate by Addition, Subtraction or Multiplication.

$numbers	=	array();
$numbers 	=	range($start, $end);

which randomly selects whether it’s Addition, Subtraction or Multiplication.

$option		=	array("0"=>"ADDITION","1"=>"SUBTRACTION","2"=>"MULTIPLICATION");
$type		=	$option[rand(0, 2)];
setup(option_type, numbers_pool)

taking the above two as its parameters, it returns an array which contains two numbers that have picked randomly to solve by Addition or Subtraction or Multiplication and what’s the actual result here, which will be matched with taken input from users.

$values		=	array();
$var		=	array_rand($numbers, 2);

rsort($var, SORT_NUMERIC); 

$values[0]	=	$var[0];
$values[1]	=	$var[1];

switch($type) {

case 'ADDITION':
$values[2]	=	'+';
$values[3]	=	$var[0] + $var[1];

$values[2]	=	'-';
$values[3]	=	$var[0] - $var[1];

$values[2]	=	'x';
$values[3]	=	$var[0] * $var[1];

$values[2]	=	'+';
$values[3]	=	$var[0] + $var[1];

The jQuery resides here to take action based on submit button, whether the result is right or wrong. The code is very simple to understand here:


function on_submit() {
  var realResult = $('#hidResult').val();
  var myResult = $('#txtResult').val();

  if( realResult == myResult) { 

  alert('You are correct!');

  return true;

  } else {

  alert('You are wrong!');


  return false;





I have already used it in my three developments what works very fine, wish this will help you too. Currently, I am working on a graphical version of it, which shows all numbers and symbols by images, what ensures more security from this mathguard.


  • 12th January, 2013:  Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Zamshed Farhan
Software Developer
Bangladesh Bangladesh
I have received B.Sc. degree in Computer Science and Engineering from Khulna University of Engineering and Technology, (KUET) Bangladesh in 2003 and Master of Business Administration in Management from University of Dhaka, Bangladesh in 2009. I started my IT career as a Software Engineer at BDCOM Online Limited, then worked for BRAC BDMail Network Limited (bracNet). At present, I am working with Application Development, Integration and Maintenance department of the International Centre for Diarrhoeal Disease Research, Bangladesh (ICDDR,B) - an international health research organization located in Dhaka, Bangladesh, dedicated to saving lives through research and treatment.

Comments and Discussions

QuestionNot an article Pin
OriginalGriff11-Jan-13 21:14
mveOriginalGriff11-Jan-13 21:14 
AnswerRe: Not an article Pin
Zamshed Farhan11-Jan-13 21:19
MemberZamshed Farhan11-Jan-13 21:19 
It seems to me that it is an article. But, I have no problem if it has been treated as Tips, because as an article or a tips, if it will be published it will be helpful for any php beginners.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.