Ad block is installed and active. Please support us by disabling it.
HomeSunday, November 19, 2017, 13:53:03
0

Creating a Math CAPTCHA with PHP - PHP Tutorial

Tuesday, February 10, 15, 10:10:02, 3 Yaers Ago Via wain, Hits: 3370
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

Prevent the error loading XML – libxml_use_internal_errors

libxml_use_internal_errors() allows you to disable standard libxml errors and enable user error handling.

Smarty - Create dynamic Web Pages with PHP & MySQL

Creating Dynamic Web Pages Using Smarty and MySQL - Requirement(s): PHP Server, MySQL

Smarty Paginate Example - Smarty Pagination with PHP

Smarty Paginate Example, Smarty Pagination with PHP. SmartyPaginate: a class/plugin for data set pagination within the Smarty template environment

Stack Overflow Clone Script - Ja2BU

Open source question and answer script written in PHP, MySQL and jQuery. Main features are: SEO friendly url's, private message system, rating system, comments system.

Upload Image with PHP [Example2]

[Example2] Tutorial - How to upload and resize image with PHP

Upload Attachment - PHP Tutorial

Upload attachment with PHP example, Requirement(s): PHP server

Send This Link PHP Script

A popup form which allows your visitors to send a referral link from your site. It features admin notification and HTML output.

PHP Trim

trim() Strip whitespace (or other characters) from the beginning and end of a string.

Link Generator PHP Script

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

Show / Hide Div with CSS

CSS Display and Visibility:The visibility property specifies whether or not an element is visible.
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.