HomeTuesday, May 23, 2017, 20:31:48
0

Creating a CAPTCHA with PHP with imagecreate

Tuesday, February 10, 15, 14:40:32, 2 Yaers Ago Via wain, Hits: 2910
This tutorial will show you how to create a form with Captcha, Requirement(s): PHP Server, GD Graphics Library
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.Imagecreate - Create a new palette based image.
Code:
$img = imagecreate(40, 20);


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


4.Displaying the random text on the captcha image
Code:
$numero = rand(100, 999);


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


index.php:
Code:
<form method="POST" action="cc.php">
<img src="captchas.php"> <br>
<input type="text" size="10" name="check"> <br>
<input type="submit" name="submit" value="submit">
</form>


captchas.php:
Code:
<?php
session_start();
//imagecreate -- Create a new palette based image
$img = imagecreate(40, 20);
//displaying the random text on the captcha image
$black = imagecolorallocate($img, 0, 0, 0);
$numero = rand(100, 999);
$number = $black . $numero;
$_SESSION['check'] = ($numero);
$white = imagecolorallocate($img, 255, 255, 255);
imagestring($img, 10, 8, 3, $numero, $white);
header ("Content-type: image/png");
imagepng($img);
?>


cc.php:
Code:
<?php
//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

Borisi Sunday, May 22, 2011, 04:02:08

Thanks for sharing. Very great and useful tutorial.

Sign In

Sponsor

Related

PHP $_GET Function

Get function in PHP

Jquery - Formatting International Phone Numbers

How to format international telephone numbers with jquery

Process Form Data with Jquery

jQuery, Process form data

PHP Forms

FREE PHP forms

Sending HTML Email With Images - PHP Tutorial

Sending HTML Email With Images - Upload Image with PHP - Creating Dynamic Webpages Using PHP and MySQL - Select, Delete, Update..

PHP imagecreate

imagecreate() returns an image identifier representing a blank image of specified size.

Creating a CAPTCHA with PHP :: PHP Tutorial

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

Upload Image With PHP

[Example3] Tutorial - How to upload image in php.

PHP $_SESSION

Session_start creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

PHP Date Examples

Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.
About Author

wain

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