Make Math Captcha in PHP

Math Captcha

Hello guys! How are you? I hope you are fine. In this post, I want to share how to make your own math captcha in PHP. But before we write the code, I want to explain what is captcha.

CAPTCHA (Completely Automated Public Turing Test To Tell Computers and Humans Apart) was first used in 2000 by Luis von Ahn, Nicholas Hopper, Manuel Blum, and John Langford of Carnegie Mellon University. They developed the first captcha to be used by Yahoo. In my own words, captcha is a program or code that use in a website to protect their form from SPAMMER.

Now, let’s try to make your own captcha code. I this post we will try make captcha in simple mathematics format, only plus, minus and time. First, we will make a captcha class. You can named it captcha-class.php and below is the code.


class captcha{
	//set property
	private $number1;
	private $number2;
	private $operator;
	function setCaptcha(){
		//set random number
		$this->number1 = rand(0, 9);
		$this->number2 = rand(0, 9);
		//set math operator list
		$listoperator = array("+", "-", "x");
		//select random operator
		$this->operator = $listoperator[rand(0,2)];
	function generateCaptcha(){
		//set number and operator
		//count the result
		if($this->operator == "+"){
			$result = $this->number1 + $this->number2;
		}elseif($this->operator == "-"){
			$result = $this->number1 - $this->number2;
			$result = $this->number1 * $this->number2;
		//set the result in session
		$_SESSION['result'] = $result;
	function showCaptcha(){
		$var = "<p>What is the result of <b>".$this->number1.$this->operator.$this->number2."</b> ?</p>";
		return $var;
	function resultCaptcha(){
		return $_SESSION['result'];

Save it! I will try to explain some codes.

  1. We make 3 class properties to define the first number, second number and math operator.
  2. Afterthat, we  make setCaptcha() method to initialize the both numbers and operator randomly.
  3. Then, we make generateCaptcha() method to get the result of the math operation and we save it in a session.

Okay, now we will make the second file, we named it index.php. In this file you can put your form. This is the sample code for index.php.


$captcha = new captcha();
	<title>Math Captcha</title>
	<form action="submit.php" method="post">
	<p>Your Name: <input type="text" name="name"></p>
	<p>Your Email: <input type="text" name="email"></p>
	<p><?php echo $captcha->showCaptcha(); ?><input type="text" name="captcha"></p>
	<p><input type="submit" value="Submit">

Save it! I will try to explain some codes.

  1. First, we call the captcha-class.php and initialize the class in $captcha variable.
  2. Afterthat, we use generateCaptcha() method to install or setup the captcha.
  3. The last is print the math captha with showCaptcha() method.

You can modify the form format as you need.

To test that your code is work properly, let’s make a file named submit.php. This file will process the input from index.php form. Below is a sample code from submit.php.

$captcha = new captcha();

//check session captcha and input from user
if($captcha->resultCaptcha() == $_POST['captcha']){
	echo "<p>Submit Complete</p>";
	echo "<p>This is you data: <br>";
	echo "Email: ".$_POST['email']."<br>";
	echo "Name: ".$_POST['name']."</p>";
	echo "<p>Captcha not valid</p>";

echo "<p><a href='index.php'>&larr; Back to form</a></p>";

Save it! I will try to explain some codes.

  1. First, we call the captcha-class.php and initialize the class in $captcha variable.
  2. In this file we don’t need to use generateCaptcha() method, because we had save the result in session so we only need use resultCaptcha() method that return the session value.

Done! Now, you can make your own mathematics captcha. Be creative!

Please give comments for this post. Thank you. :)

Download Source Code



One thought on “Make Math Captcha in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>