Creating a CAPTCHA
It's a fairly long code, so I'll explain it afterwards.
PHP 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
<?php
session_start();
$alpanumb = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstufwxyz1234567890";
$code = substr(str_shuffle($alpanumb), 0, 5);
$num = "1230";
$rand = substr(str_shuffle($num), 0, 2);
$coded = base64_encode($code);
$_SESSION['CAPTCHA'] = $code;
$width = 118;
$height = 27;
$im = imagecreatefrompng("./captcha.PNG");
$black = imagecolorallocate($im, rand(1, 255), rand(1, 255), rand(1, 255));
// grid
$grid_color = imagecolorallocate($im, 175, 0, 0);
$number_to_loop = ceil($width / 20);
for($i = 0; $i < $number_to_loop; $i++) {
$x = ($i + 1) * 20;
imageline($im, $x, 0, $x, $height, $grid_color);
}
$number_to_loop = ceil($height / 10);
for($i = 0; $i < $number_to_loop; $i++) {
$y = ($i + 1) * 10;
imageline($im, 0, $y, $width, $y, $grid_color);
}
// random lines
$line_color = imagecolorallocate($im, 0, 0, 0);
for($i = 0; $i < 30; $i++) {
$rand_x_1 = rand(0, $width - 1);
$rand_x_2 = rand(0, $width - 1);
$rand_y_1 = rand(0, $height - 1);
$rand_y_2 = rand(0, $height - 1);
imageline($im, $rand_x_1, $rand_y_1, $rand_x_2, $rand_y_2, $line_color);
}
imagestring($im, 5, $rand, 10, $code, $black);
header("Content-type: image/png");
header("no-cache");
imagepng($im);
echo "<BR><BR>$random1";
?>
Alrighty then, you can call that whatever you wish.
-----------------------HOW TO USE-----------------------
**PLEASE NOTE: You must create your own image or use the one below:
Just recolour it to your needs. To show the image, all you must do it on your page (not the php page) put this where you want it:
Code
<img src="captcha.php">
Just change captcha with the file name. Sometimes the font color is not readable (or at least for mine) so instead change the above image code to:
Code
<img src="../captcha.php" onclick="this.src='../captcha.php?time=' + (new Date()).getTime();">
-----------------------ABOUT-----------------------
Pretty self explanatory really, everything is in English. But, I'll explain the most important things.
$im, specifies the image, allowing modifications to happen.
$black, specifies the font colour, don't know why it's called black.
That's basically it. Nothing more too it. Hope this helps!