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
|
<?php session_start();
$captcha_length = $captcha_length?$captcha_length:6;
if($_GET['length']) $captcha_length = $_GET['length'];
$captcha_characters = array();
$captcha_number = array(1,2,3,4,5,6,7,8,9);
$captcha_letter = array('a','b','c','d','e','f','g','h','i','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z');
/** * DEFAULT NUMBER OF LETTERS AND NUMBERS */ $nb_numbers = ceil($captcha_length/2); $nb_letters = $captcha_length-ceil($captcha_length/2);
/** * NUMBERS IN THE CAPTCHA */
if($_GET['format']=='numbers') { $captcha_characters = array(); $nb_numbers = $captcha_length; }
$captcha_rand_number = array_rand($captcha_number, $nb_numbers);
if(!is_array($captcha_rand_number)) // If you are picking only one entry, array_rand() returns the key for a random entry. { $captcha_rand_number = array($captcha_rand_number); }
//var_dump($captcha_rand_number);
foreach($captcha_rand_number as $value) { $captcha_characters[] = $captcha_number[$value]; }
/** * LETTERS IN THE CAPTCHA */
if($_GET['format']=='letters') { $captcha_characters = array(); $nb_letters = $captcha_length; }
if(count($captcha_characters)<$captcha_length) { $captcha_rand_letter = array_rand($captcha_letter, $nb_letters); if(!is_array($captcha_rand_letter)) // If you are picking only one entry, array_rand() returns the key for a random entry. { $captcha_rand_letter = array($captcha_rand_letter); } //var_dump($captcha_rand_letter);
foreach($captcha_rand_letter as $value) { $captcha_characters[] = $captcha_letter[$value]; }
} //var_dump($captcha_characters);
/** * SHUFFLE AND IMAGE */
shuffle($captcha_characters);
foreach($captcha_characters as $value) { $captcha_img_value .= $value; }
$_SESSION['captcha_img_string'] = $captcha_img_value;
// Create a 100*30 image $im = imagecreate(110, 28);
// White background and blue text $bg = imagecolorallocate($im, 238, 238, 238); $textcolor = imagecolorallocate($im, 0, 0, 255);
// Write the string at the top left imagestring($im, 5, 10, 6, $captcha_img_value, $textcolor);
// Output the image header('Content-type: image/png');
imagepng($im); imagedestroy($im); ?>
|