ValidateCode.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
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
/**
* Verification Code Class
* @author Staven
*
*/
Class validatecode{
Private $width; Verify the width of the code picture
Private $height; Verify the height of the code picture
Private $checkCode; Verification Code characters
Private $codeNum; Number of verification code characters
Private $font; Font
Private $fontSize; Font size
Private $fontColor; Font Color
Private $image; Verification Code Handle
Private $charset; verifying character random factors
Constructor method Initialization
function __construct ($width =100, $height =20, $codeNum =4, $fontSize = 12) {
$this->width = $width;
$this->height = $height;
$this->codenum = $codeNum;
$this->fontsize = $fontSize;
$this->font = dirname (__file__). '/font/elephant.ttf ';
$this->charset = ' abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789 '; Provide a random factor
$this->checkcode = $this->createcheckcode ();
}
/* For output verification code picture, and the Verification Code information is written to the session */
function __tostring () {
/* Add to session, store subscript as code */
$_session ["code"] = Strtoupper ($this->checkcode);
$this->outimage ();
Return ';
}
/* Generate random Verification code characters */
Private Function Createcheckcode () {
for ($i =0; $i < $this->codenum; $i +) {
$this->checkcode. = $this->charset{rand (0, strlen ($this->charset)-1)};
}
return $this->checkcode;
}
/* Output Verification code */
Private Function Outimage () {
$this->getcreateimage ();
$this->createline ();
$this->outputtext ();
$this->outputimage ();
}
/* Generate Verification Code background picture */
Private Function Getcreateimage () {
$this->image = Imagecreatetruecolor ($this->width, $this->height);
$color = Imagecolorallocate ($this->image, Mt_rand (157,255), Mt_rand (157,255), Mt_rand (157,255));
Imagefilledrectangle ($this->image,0, $this->height, $this->width,0, $color);
}
/* Write a text message to the handle picture */
Private Function Outputtext () {
$_x = $this->width/$this->codenum;
for ($i =0; $i < $this->codenum; $i +) {
$this->fontcolor = imagecolorallocate ($this->image,mt_rand (0,156), Mt_rand (0,156), Mt_rand (0,156));
Imagettftext ($this->image, $this->fontsize,mt_rand ( -30,30), $_x * $i +mt_rand (1,5), $this->height/1.4, $thi S->fontcolor, $this->font, $this->checkcode[$i]);
}
}
/* Create snowflake pattern */
Private Function Createline () {
for ($i =0; $i <6; $i + +) {
$color = Imagecolorallocate ($this->image,mt_rand (0,156), Mt_rand (0,156), Mt_rand (0,156));
Imageline ($this->image,mt_rand (0, $this->width), Mt_rand (0, $this->height), Mt_rand (0, $this->width), MT _rand (0, $this->height), $color);
}
for ($i =0; $i <100; $i + +) {
$color = Imagecolorallocate ($this->image,mt_rand (200,255), Mt_rand (200,255), Mt_rand (200,255));
Imagestring ($this->image,mt_rand (1,5), Mt_rand (0, $this->width), Mt_rand (0, $this->height), ' * ', $color);
}
}
/* Generate CAPTCHA Picture */
Private Function Outputimage () {
Header (' content-type:image/png ');
Imagepng ($this->image);
Imagedestroy ($this->image);
}
/* Destroy the handle and release the resource */
function __destruct () {
Imagedestroy ($this->image);
}
}
imagecode.php
1
2
3
4
Session_Start (); Open session
Require_once ' ValidateCode.class.php ';
echo New Validatecode ();
image.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
Verification Code
/** for */
Session_Start (); Open session
if (Isset ($_post [' submit ')]) {//To determine the execution of the user after submission
/* Determine if the string in the form entered by the user is the same as that in the CAPTCHA picture */
if (Strtoupper (Trim ($_post ["code])) = = = $_session [' code ']) {//If the verification code output is successful
Echo ' Verification code entered successfully
' ; Output a successful message
} else {//If the CAPTCHA input fails
echo ' Verification code input Error!!
' ; Output failed input information
}
}
?>
From for notes (Wiz)