Ad block is installed and active. Please support us by disabling it.
HomeThursday, September 21, 2017, 03:22:59
0

Creating a Math CAPTCHA with PHP - PHP Tutorial

Tuesday, February 10, 15, 10:10:02, 3 Yaers Ago Via wain, Hits: 3048
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 form with file upload

Advanced Contact Form with email and form field validation. It allows your visitors to send you an email directly from your website.

Comments 1.1. :: PHP Script

This script allows your visitors to add comments to any single page on your web site.

HTML Button Generator

This script will allow your visitors to create HTML Buttons online. To use the "html button generator" script completely free , the "powered by" link at the bottom of the pages must stay intact.

Adsense crawler errors and robots.txt

How to fix Google AdSense crawler errors in robots.txt

Imageline

Draws a line between the two given points.

How to insert new line (IP Address) with fwrite

How to insert new line (IP Address) with fwrite, Get IP address and write to file.

PHP explode

Explode — Split a string by string.

Math Captcha V6

PHP Math Captcha Image Version 6

PHP Cookies and Sessions Tutorial

PHP Cookies and Sessions Tutorials

Smarty - Create dynamic Web Pages with PHP & MySQL

Creating Dynamic Web Pages Using Smarty 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.