SnippetsProject Code

Simple Image CAPTCHA

Description

This is your most basic version of an image CAPTCHA.

Snippet

image.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
header("Content-type: image/png");
$string "abcdefghijklmnopqrstuvwxyz0123456789";
for($i=0;$i<6;$i++){
	$pos rand(0,36);
	$str .= $string{$pos};
}

$img_handle ImageCreate (6020) or die ("Cannot Create image");
//Image size (x,y)
$back_color ImageColorAllocate($img_handle255255255);
//Background color RBG
$txt_color ImageColorAllocate($img_handle000);
//Text Color RBG
ImageString($img_handle3150$str$txt_color);
Imagepng($img_handle);

session_start();
$_SESSION['img_number'] = $str;
?> 

form.php

1
2
3
4
5
<form action="result.php" method="post">
	<img alt="Random Number" src="image.php"> 
	<input type="text" name="num"><br>
	<input type="submit" name="submit" value="Check">
</form> 

result.php

1
2
3
4
5
6
7
8
9
10
<?php
session_start();
if($_SESSION['img_number'] != $_POST['num']){
	echo'The number you entered doesn't match the image.<br>
	<a href="form.php">Try Again</a><br>';
}else{
	echo'The numbers Match!<br>
	<a href="form.php">Try Again</a><br>;
}
?>

Example

Comments

ADD YOUR COMMENT:

Formatting

Comments.
neilfurry October 10th 2007
well I think mr.anonymous doesnt know the real meaning of "SIMPLE", what he is trying to imply is an ADVANCE type of captcha...lol..
Gerry July 17th 2007
is there anyway of styling the image?
jl July 1st 2007
if you want to generate a new image everytime you visit the "form" page, use this in begining of "image" code:

header( "Cache-Control: no-store, no-cache, must-revalidate" );
header( "Cache-Control: post-check=0, pre-check=0", false );
header( "Pragma: nocache" );
Anonymous May 16th 2007
It is not secure at all. No random lines, no distorted text... heh. Even the most basic bot can crack this, no sweat.
Admin March 26th 2007 (Website)
Well... Anonymous That is one reason it is called a "Simple Image CAPTCHA". It is simple and it works like the more fancy ones.

I will try to create a fancier one though for future reference.
Anonymous February 22nd 2007
Well, unlike most captchas, there aren't any lines or anything, difference in colors, or anything that makes it seem "secure".
SomaFM February 16th 2007
"Anonymous", when you post a comment like that you should expand on why you think that.
g February 13th 2007
how can u modify the the look of the image?
Admin February 13th 2007 (Website)
It depends on what you mean by "Look and Feel" Do you want the image to have different text?
Anonymous February 13th 2007
Doesn't look very secure.
SomaFM February 9th 2007
Great code. But is there anyway to end the session so the person can't use the back button and use the same code again in the form? I've tried placing session_destroy(); in my image.php script right before it creates a session. Also, I tried placing it in the page that gets executed by the form.
SomaFM February 9th 2007
Nevermind. session_unset() does the trick :) Thanks for the great code!
Admin February 9th 2007 (Website)
Your Very Welcome!
Wondering February 8th 2007
Can we see an example of what the final captcha will look like?

Login



Did you want to add your own codes?
Create an Account its fast and easy!

Did you forget your username/password?
Have us reset it it for you!

Information

2.61 / 5 (370 votes)

9,135 views

2 downloads New

Code Tools

Bookmark and Share

Programmer

admin

Posted on: Jan 01, 2008

View all of Ryan's snips