Click here to Skip to main content
15,868,016 members
Articles / Security
Tip/Trick

MathGuard: A Very Simple Alternative to Regular CAPTCHA for Beginners

Rate me:
Please Sign up or sign in to vote.
4.67/5 (5 votes)
11 Jan 2013CPOL2 min read 16.7K   344   3   2
MathGuard is a security option for beginners, developed by PHP and jQuery

Introduction

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

Description

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

Or:

Image 3

Using the Code

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

PHP
include('class.mathguard.php');
$objMG = new MathGuard; 

And the object objMG called three functions as:

PHP
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.

PHP
$numbers	=	array();
$numbers 	=	range($start, $end);
shuffle($numbers);
PHP
option_type() 

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

PHP
$option		=	array("0"=>"ADDITION","1"=>"SUBTRACTION","2"=>"MULTIPLICATION");
$type		=	$option[rand(0, 2)];
PHP
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.

PHP
$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];
break;

case 'SUBTRACTION':
$values[2]	=	'-';
$values[3]	=	$var[0] - $var[1];
break; 

case 'MULTIPLICATION':
$values[2]	=	'x';
$values[3]	=	$var[0] * $var[1];
break; 

default:
$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:

JavaScript
<script>
$(document).ready(function(){
$('#btnSubmit').click(on_submit);
}); 

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!');

  $('#txtResult').val("");

  return false;

  } 

  }

</script> 

Conclusion

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.

History

  • 12th January, 2013:  Initial post

License

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


Written By
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 20:14
mveOriginalGriff11-Jan-13 20:14 
AnswerRe: Not an article Pin
Zamshed Farhan11-Jan-13 20:19
Zamshed Farhan11-Jan-13 20:19 
Hello.
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.