Ad block is installed and active. Please support us by disabling it.
HomeSaturday, January 20, 2018, 16:40:48
0

Creating a Math CAPTCHA with PHP - PHP Tutorial

Tuesday, February 10, 15, 10:10:02, 3 Yaers Ago Via wain, Hits: 3781
This tutorial will show you how to create a form with Math Captcha.
Download source code (tutorial) - Math Captcha Script
http://phpform.net/download.php?f=math-captcha.zip

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.
Code:
<?php session_start(); ?>


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


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


4. Displaying the random text on the captcha image.
Code:
//value 1
$numeroa = rand(1, 9);
//value2
$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.
Code:
if(($_POST['check']) == $_SESSION['check'])
{ echo 'Input OK';
}else{
echo 'Input Wrong';
}


Files

black.png
http://phpform.net/images/black.png

index.php
Code:
<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">
</form>


captcha.php
Code:
<?php session_start();
$img = imagecreatefrompng('black.png');
//value 1
$numeroa = rand(1, 9);
//value2
$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);
?>


cc.php
Code:
<?php
session_start();
//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';
}else{
echo 'Input Wrong';
}


example 1:
http://phpform.net/captcha.php

example 2:
http://phpform.net/captcha2.php

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

Sponsor

Related

PHP Fwrite : fwrite

php write to file - how to write data to a file using the php frwite function

Syntax highlighting in Smarty

Syntax highlighting for Smarty - Google Code Prettify

Creating a CAPTCHA with PHP :: PHP Tutorial

Captcha Image, This tutorial will show you how to create a form with Captcha.

PHP filesize

The filesize function returns the size of the specified file.

GuestBook PHP Script

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

Jquery PHP Contact Form

This script allows your visitors to send you an email directly from your website.

Timed Redirect JavaScript

Timed redirect javaScript/PHP example

PHP Str_replace - PHP Tutorial

This function returns a string or an array with all occurrences of search in subject replaced with the given replace value.

Images In Textarea - CSS

Tutorial Images in Textarea

MySQL Inner Join

MySQL Inner Join - Creating Dynamic Webpages Using PHP and MySQL, Requirement(s): PHP Server, MySQL
About Author

wain

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