SnippetsProject Code

Advanced Image CAPTCHA

Description

This is your basic CAPTCHA that you can find on many web sites to HELP stop robots.

Snippet

image.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
// Font directory + font name
$font 'fonts/Disney.ttf';
// Total number of lines
$lineCount 40;
// Size of the font
$fontSize 40;
// Height of the image
$height 50;
// Width of the image
$width 150;
$img_handle imagecreate ($width$height) or die ("Cannot Create image");
// Set the Background Color RGB
$backColor imagecolorallocate($img_handle255255255);
// Set the Line Color RGB
$lineColor imagecolorallocate($img_handle175238238);
// Set the Text Color RGB
$txtColor imagecolorallocate($img_handle135206235);

// Do not edit below this point
$string "abcdefghijklmnopqrstuvwxyz0123456789";
for($i=0;$i<6;$i++){
	$pos rand(0,36);
	$str .= $string{$pos};
}
$textbox imagettfbbox($fontSize0$font$str) or die('Error in imagettfbbox function');
$x = ($width $textbox[4])/2;
$y = ($height $textbox[5])/2;
imagettftext($img_handle$fontSize0$x$y$txtColor$font $str) or die('Error in imagettftext function');
for($i=0;$i<$lineCount;$i++){
	$x1 rand(0,$width);$x2 rand(0,$width);
	$y1 rand(0,$width);$y2 rand(0,$width);
	imageline($img_handle,$x1,$y1,$x2,$y2,$lineColor);
}
header('Content-Type: image/jpeg');
imagejpeg($img_handle,NULL,100);
imagedestroy($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.
CasTex November 23rd 2009 (Website)
Works fine on me.
Admin February 15th 2008 (Website)
That is probably because there was an error on the next page, I fixed that problem, and should work now (the quote wasn't escaped)
Trium December 11th 2007
The image captcha isn't working when I entered the number.
Admin October 21st 2007 (Website)
This snippet was used on our site to stop/slow down the spam. We apologize for all the spam lately.

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

3.16 / 5 (271 votes)

11,889 views

8 downloads New

Code Tools

Bookmark and Share

Programmer

admin

Posted on: Jan 01, 2008

View all of Ryan's snips