HomeMonday, June 26, 2017, 10:24:28
0

Creating a CAPTCHA with PHP with imagecreate

Tuesday, February 10, 15, 14:40:32, 2 Yaers Ago Via wain, Hits: 3112
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

ShoutBox 1.1. PHP Script

ShoutBox allows your users to post short messages on your site. The script is lightweight and very easy to install and use.

Upload File with PHP

Basic file uploading with PHP

Contact Us Form With Captcha

A contact us form with email and form field validation. It allows your visitors to send you an email directly from your website. Features: Captcha Image, HTML output.

PHP File Create

Create and write data to a file using the php frwite function.

View content in a zip file with PHP

List all files in a zip archive with PHP

Captcha V1 - Free PHP Script

An anti-spam form protection image written in PHP, Version: 1.1.

PHP form with image upload

Advanced Contact Form with email and form field validation.It allows your visitors to send you an email directly from your website.Features: Image Upload, HTML Output, Captcha Image.

Timed Redirect JavaScript

Timed redirect javaScript/PHP example

Create dynamic Web Pages with PHP & MySQL

Create dynamic Web Pages with PHP & MySQL, Requirement(s): PHP Server, MySQL

Inserting form data in MySql table

Inserting form data in MySql table with PHP
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.