How to generate CAPTCHA?

By admin at 30 June, 2008, 1:35 pm

CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart) is a simple test to determine if a user is a computer or a human. It’s is a type of challenge-response test used in computing to ensure that the response is not generated by a computer.

CAPTCHA helps to prevent spam abuse on the websites. So if you use CAPTCHA on your web site forms, this can help in stopping some bots and making life harder for other bots in accessing or using your forms. In brief the CAPTCHA protection works by generating a random string, writing it to an image, then storing the string inside of a session or by some other method. This is then checked when the form is submitted.

Here is the full code demonstrating how to make your own simple CAPTCHA protection using PHP.

class.captcha.php [source-code]

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
 
<?php
 
class captcha
{
	var $charArray = array();
 
	function captcha()
	{
		session_start();
	}
 
	function make_seed()
	{
   		list($usec, $sec) = explode(' ', microtime());
   		return (float) $sec + ((float) $usec * 100000);
	}
 
	function generateCaptcha()
	{
		//$captcha = array();
		for($i = 0;$i<6;$i++)
		{
			$p = rand(1,3);
			if($p == 1)
			{
				$this->charArray[$i]=chr(rand(65,90));
			}
			if($p == 2)
			{
				$this->charArray[$i]=chr(rand(97,122));
			}
			if($p == 3)
			{
				$this->charArray[$i]=chr(rand(48,57));
			}
			$tempStr .= $this->charArray[$i];
		}
		$_SESSION['captcha'] = md5(strtolower($tempStr));
		return $this->charArray;;
	}
 
	function randomCharsGenerator()
	{
		$ranCharArray = 
 
array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d"
 
,"e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9",
 
"0");
 
		$sizeOfArray = sizeof($ranCharArray);
 
		for($i=0; $i<6; $i++)
		{
			srand($this->make_seed());
			//$ascii = rand(64,90);
			//$this->charArray[$i] = chr($ascii);
			$temp = rand(0, $sizeOfArray-1);
			$this->charArray[$i] = $ranCharArray[$temp];
			$tempStr .= $this->charArray[$i];
		}
		$_SESSION['captcha'] = md5(strtolower($tempStr));
		return $this->charArray;
	}
 
	function randomDirtGenerator($dirt)
	{
		$ranDirtArray = array(".",":","~","`","'","-","_");
 
		$dirtArray = array();
		$sizeOfArray = sizeof($ranDirtArray);
 
		for($i=0; $i<$dirt; $i++)
		{
			srand($this->make_seed());
			$temp = rand(0,$sizeOfArray-1);
			$dirtArray[$i] = $ranDirtArray[$temp];
		}
		return $dirtArray;
	}
 
	function getValue()
	{
		return $this->charArray;
	}
 
	function captchaMaker($width, $height)
	{
		$canvas = ImageCreate($width, $height);
 
		$red = ImageColorAllocate($canvas,0xe4,0x2e,0x2e);
		$white = ImageColorAllocate($canvas,0xff,0xff,0xff);
		$gray = ImageColorAllocate($canvas,0x80,0x80,0x80);
		$blue = ImageColorAllocate($canvas,0x33,0x66,0x99);
		$black = ImageColorAllocate($canvas,0x00,0x00,0x00);
 
		ImageFilledRectangle($canvas,1,1,$width-2,$height-2,$white);
 
		//$ranChars = $this->randomCharsGenerator();
		$ranChars = $this->generateCaptcha();
		$ranDirts = $this->randomDirtGenerator(100);
 
		$boundaryX = abs($width/6);
		$boundaryY = $height;
 
		//$font = array("times","arial","comic");
 
		for($i=0; $i<sizeof($ranChars);$i++)
		{
			srand($this->make_seed());
			$ranAngle = rand(-45,45);
			$ranX = rand(($i*$boundaryX)+30,(($i+1)*$boundaryX)-30);
			$ranY = rand(30, $boundaryY-30);
			$ranFont = rand(0,sizeof($font)-1);
			ImageTTFText($canvas,20,$ranAngle,$ranX,$ranY,$black,"arial.ttf",$ranChars[$i]);
		}
 
		$color=array("$red","$blue","$gray","$black");
		$fontSize=array(6,7,8,9,10);
		for($i=0; $i<sizeof($ranDirts);$i++)
		{
			$ranAngle = rand(-60,60);
			$ranX = rand(5, $width);
			$ranY = rand(5, $height);
			srand($this->make_seed());
			$ranColor = rand(0,sizeof($color)-1);
			$ranFontSize = rand(0,sizeof($fontSize)-1);
 
 
ImageTTFText($canvas,$fontSize[$ranFontSize],$ranAngle,$ranX,$ranY,$color[$ranColor],"arial.ttf",$ranDirts[$i]);
		}
		header("Expires: Wed, 1 Jan 1990 00:00:00 GMT");
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
		header("Cache-Control: no-store, no-cache, must-revalidate");
		header("Cache-Control: post-check=0, pre-check=0", false);
		header("Pragma: no-cache");
		header('Content-type: image/jpeg');
		ImageJPEG($canvas);	
		//return $canvas;	
	}
}
$a = new captcha();
$canvas=$a->captchaMaker(350,100);
 
?>

Spread the Words!




No comments yet.

Sorry, the comment form is closed at this time.