HomeWednesday, September 28, 2016, 05:06:29
54

Creating a CAPTCHA with PHP :: PHP Tutorial

Tuesday, February 10, 15, 14:45:21, 2 Yaers Ago Via wain, Hits: 3080
Captcha Image, This tutorial will show you how to create a form with Captcha.
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:
$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


black.png
http://phpform.net/images/black.png

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();
//imagecreatefrompng :: create a new image
//from file or URL
$img = imagecreatefrompng('black.png');
//displaying the random text on the captcha
$numero = rand(100, 999);
$_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, $numero, $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 2:
http://phpform.net/captcha2.php
Collections of PHP, Best PHP, Ajax, ASP Scripts. Submit a PHP Script! Free and commercial script collection for Windows and Unix/Linux.

Svolte Monday, June 7, 2010, 16:32:25

Thanks a bunch for this script, its really helpful! :)

sivaramakrishnan Monday, July 12, 2010, 06:04:44

Nice,its very useful for me to creating it.

Alif Tuesday, July 27, 2010, 21:05:13

it's nice tutorial, thanks for share the code

Padmashri Friday, August 27, 2010, 06:14:39

Thanks for this php code.it's very helpful for me.

Jeremy Wednesday, September 15, 2010, 09:06:24

thanks for this code ! Just how we can improve the security of this script and generally any captcha script ?

Mihail Tuesday, December 28, 2010, 14:52:12

nice tutorial,
//clear session to prevent re-use

unset($_SESSION[\'check\']);

//end of cc.php

jigibau Tuesday, December 28, 2010, 15:50:59

This is a very nice tutorial. Thanks ..

Michael Pedersen Wednesday, December 29, 2010, 10:21:04

Excellent script for beginners like me. I spend sooo much time on other scripts, but this one worked instantly.
Thanks a lot - Michael.

mimija Saturday, January 22, 2011, 05:41:49

Very useful tut, thank you :)

Ronald Paypon Sunday, February 27, 2011, 12:17:10

Simple but wonderful! Thanks a lot for this basic captcha code.

lakshitha Wednesday, March 2, 2011, 13:55:32

i got an issue pls help smone.. i did alert the session variable($_SESSION[\'check\']) in javascript but it alert the prvious captcha. not the current image captcha.(i got this issue whn i need to validate my form on js). here the code snippet

var imageVarification = $(\"#captcha\").val();
var imagecode = ;
alert(imagecode);

lucky Saturday, March 19, 2011, 08:38:31

Thanks a lot!!! this was really helpful

micho Wednesday, May 4, 2011, 06:19:34

Great Tutorial... Thanks for Sharing.

will Tuesday, July 12, 2011, 11:36:02

Thanks for code also. It worked and was very helpful for me.

ronaldo Friday, August 12, 2011, 15:53:49

thanks admin

nero Wednesday, August 31, 2011, 18:14:04

Thanks for share the code

Michael Wednesday, October 12, 2011, 04:53:24

Very useful code, thanks much.

Gane Wednesday, December 28, 2011, 17:07:34

Thanks for this great tutorial, it was really usefull

Matthew Sunday, February 12, 2012, 18:39:24

Really useful bit of code, thanks for that.

blogger Wednesday, April 18, 2012, 11:28:49

Thanks for the very useful article.

Sign In

Web Hosting
First Month Free with Coupon Code phpmonth
Unlimited Domains - Unlimited Storage
Unlimited Transfer - Unlimited Email
Related

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.

Creating a Math CAPTCHA with PHP - PHP Tutorial

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

Creating a CAPTCHA with PHP with imagecreate

This tutorial will show you how to create a form with Captcha, Requirement(s): PHP Server, GD Graphics Library

Create dynamic Web Pages with PHP & MySQL

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

Password Protect With PHP

Password protect tith PHP - tutorial

Syntax highlighting in Smarty

Syntax highlighting for Smarty - Google Code Prettify

Preg_Match - PHP Email Validation

Validate email with preg_match in php

FeedBack Form with Captcha

It allows your visitors to send you an email directly from your website.

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.

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.
About Author

wain

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