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

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
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

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

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

wain

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