Click here to Skip to main content
13,353,106 members (81,049 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 11 Jan 2013

MathGuard: A Very Simple Alternative to Regular CAPTCHA for Beginners

, 12 Jan 2013
Rate this:
Please Sign up or sign in to vote.
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:

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:


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.

You may also be interested in...

Comments and Discussions

QuestionNot an article Pin
OriginalGriff11-Jan-13 21:14
mvpOriginalGriff11-Jan-13 21:14 
AnswerRe: Not an article Pin
S.M. Zamshed Farhan11-Jan-13 21:19
memberS.M. Zamshed Farhan11-Jan-13 21:19 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 12 Jan 2013
Article Copyright 2013 by Zamshed Farhan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid