Adding a Simple Math Captcha to your Form in PHP

Date Posted: April 27, 2014, 12:45 pm


Math_Captcha

First is we need to create a new session by adding this code:

Second, we generate the numbers that we will be using for the Math Captcha:

1
2
3
$num1=rand(1,9); //Generate First number between 1 and 9  
$num2=rand(1,9); //Generate Second number between 1 and 9  
$captcha_total=$num1+$num2;

Third, we set the Math question into a variable (This his how the question will look like.)

1
$math = "$num1"." + "."$num2"." =";

Fourth, we set the properties of our Match Captcha image.

1
2
3
4
$image = imagecreatetruecolor(120, 30); //Change the numbers to adjust the size of the image
$black = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocate($image, 0, 100, 90);
$white = imagecolorallocate($image, 0, 26, 26);

Finally, We create the image:

1
2
3
4
5
imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 20, 0, 20, 25, $color, $font, $math );//Change the numbers to adjust the font-size

header("Content-type: image/png");
imagepng($image);

Save this as captcha.php, here is the full code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
session_start();

$num1=rand(1,9); //Generate First number between 1 and 9  
$num2=rand(1,9); //Generate Second number between 1 and 9  
$captcha_total=$num1+$num2;  

$math = "$num1"." + "."$num2"." =";  

$_SESSION['rand_code'] = $captcha_total;

$font = 'mmobuyit-captcha-fonts/Arial.ttf';

$image = imagecreatetruecolor(120, 30); //Change the numbers to adjust the size of the image
$black = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocate($image, 0, 100, 90);
$white = imagecolorallocate($image, 0, 26, 26);

imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 20, 0, 20, 25, $color, $font, $math );//Change the numbers to adjust the font-size

header("Content-type: image/png");
imagepng($image);

Next step is to create our form and a some validations.

Validation:

1
2
3
4
5
6
7
8
9
10
11
12
13
$errors = array();

if ($_POST['registerUser']) {

    if (empty($errors) === true) {

        if (empty($_POST['captcha_entered'])) {
            $errors[] = '<p class = "input-error">Please answer the Captcha Question</p>';
        } elseif ($_REQUEST['captcha_entered']!=$_SESSION['rand_code']) {
            $errors[] = '<p class = "input-error">Your answer to the Math Question is incorrect.</p>';
        }
    }  
}

Sample Form:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<form method="post" class="register" id = "register-ajax">

<?php
if (empty($_POST) === false && empty($errors) === true) {
       
    echo 'Success';
       //Run some codes here.
           
} else if (empty($errors) === false) {
       
    echo output_errors($errors);
}
?>
    <p>
        <img src="captcha.php" />
        <input name="captcha_entered" style = "width: 100px !important;" type="text" id="captcha_entered" size="5" maxlength="2" placeholder = "Answer" />
        <input type="hidden" name="captcha_total" id="captcha_total" value="<?php echo $_SESSION['rand_code']; ?>" />
    </p>
    <p>
        <input type="submit" class="button" name="registerUser" value="<?php _e( 'Register' ); ?>" />
    </p>
</form>

Checkout my other PHP Tutorials by following this LINK

About author


Carl Victor Fontanos

A fine gentleman specializing in front-end and back-end development with extensive experience building high performance web applications that keeps users engaged and help businesses grow.

 
Hire Me