HomeMonday, June 18, 2018, 15:26:41
0

Creating a CAPTCHA with PHP with imagecreate

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

Explode — Split a string by string.

php Array

These functions allow you to interact with and manipulate arrays in various ways. Arrays are essential for storing, managing, and operating on sets of variables.

Captcha V1 - Free PHP Script

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

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.

PHP str_replace

str_replace - This function returns a string or an array with all occurrences of search in subject replaced with the given replace value

Tell A Friend with Captcha

This form allows your visitors to suggest your site to a friend. It features an HTML email format and an admin notification email.

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.

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 Redirect

Header (location) redirect must be processed before any output to the browser.

PHP Write to File

PHP Write to File :: PHP Tutorial, requirement(s): PHP server
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.