HomeSunday, June 24, 2018, 01:36:34

Creating a Math CAPTCHA with PHP - PHP Tutorial

Tuesday, February 10, 15, 10:10:02, 3 Yaers Ago Via wain, Hits: 6756
This tutorial will show you how to create a form with Math Captcha.
Download source code (tutorial) - Math Captcha Script

Requirement(s): PHP Server

1. Sessions in PHP are started by using the session_start() function. Like the setcookie( ) function, the session_start( ) function must come before any HTML, including blank lines, on the page.
<?php session_start(); ?>

2. Imagecreatefrompng :: create a new image from file or URL.
$img = imagecreatefrompng('black.png');

3. The function imagecolorallocate creates a color using RGB (red,green,blue) format.:
$white = imagecolorallocate($img, 255, 255, 255);

4. Displaying the random text on the captcha image.
//value 1
$numeroa = rand(1, 9);
$numerob = rand(1, 9);
$numero = $numeroa + $numerob;
$display = $numeroa . '+' . $numerob;

5. Check if the security code and the session value are not blank and if the input text matches the stored text.
if(($_POST['check']) == $_SESSION['check'])
{ echo 'Input OK';
echo 'Input Wrong';



<form method="POST" action="cc.php">
<img src="captcha.php"> <br>
<input type="text" size="10" name="check"> <br>
<input type="submit" name="submit" value="submit">

<?php session_start();
$img = imagecreatefrompng('black.png');
//value 1
$numeroa = rand(1, 9);
$numerob = rand(1, 9);
$numero = $numeroa + $numerob;
$display = $numeroa . '+' . $numerob;
$_SESSION['check'] = $numero;
//The function imagecolorallocate creates a
//color using RGB (red,green,blue) format.
$white = imagecolorallocate($img, 255, 255, 255);
imagestring($img, 10, 8, 3, $display, $white);
header ("Content-type: image/png"); imagepng($img);

//Sessions in PHP are started by using the
//session_start() function.
//Like the setcookie( ) function,
//the session_start function must come before any HTML,
//including blank lines, on the page.session_start();
//Check if the security code and
//the session value are not blank
//and if the input text matches the stored text
if(($_POST['check']) == $_SESSION['check']) {
echo 'Input OK';
echo 'Input Wrong';

example 1:

example 2:

selen Wednesday, December 15, 2010, 20:57:34

This is very good article and useful for me, thanks

Daniel Tuesday, February 22, 2011, 20:27:10

You don\'t really need to use a black image file to initialize the image, you can just fill the image with imagefilledrectangle()

Sign In


Email to Friend - PHP Script

This form allows your visitors to suggest your site to a friend. It features an HTML email format and an admin notification email.

Addslashes - Stripslashes

addslashes — quote string with slashes, stripslashes — un-quotes a quoted string


Getimagesize function will determine the size of any given image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML IMG tag and the correspondant HTTP content type.

Upload and Resize an Image with PHP

Tutorial - How to upload and resize image with PHP

Adsense crawler errors and robots.txt

How to fix Google AdSense crawler errors in robots.txt

PHP highlight_string - PHP Tutorial

Outputs or returns a syntax highlighted version of the given PHP code using the colors defined in the built-in syntax highlighter for PHP. Requirement(s): PHP Server.

Convert pdf to image - mpdf

Convert pdf file to jpg with php and mpdf

GuestBook PHP Script

Allow visitors to leave messages on your site. It features easy install, admin notification, and control panel to remove unwanted entries.

Link Base L.1.1.

This script allows you to manage your links using a MySQL database. It features easy install, user submission, and an admin panel to add, edit and remove categories and links. The script is easy to customize to suit your needs.

Captcha V1 - Free PHP Script

An anti-spam form protection image written in PHP, Version: 1.1.
About Author


Date Registered: Sun, Feb 8, 15
Last Time Online: Sun, Mar 18, 18
So far has created 115 entries.
This author has not yet filled in any details.