|
<? Php
Class ValidationCode
{
// Attributes
Private $ width;
Private $ height;
Private $ codeNum;
Private $ image;
Private $ disturbColorNum; // Number of interference elements
Private $ checkCode;
Function _ construct ($ width = 80, $ height = 20, $ codeNum = 4)
{
$ This-> width = $ width;
$ This-> height = $ height;
$ This-> codeNum = $ codeNum;
$ Number = floor ($ width * $ height/15 );
If ($ number> 240-$ codeNum)
{
$ This-> disturbcolornum= 240-$ codeNum;
} Else
{
$ This-> disturbColorNum = $ number;
}
$ This-> checkCode = $ this-> createCheckcode ();
}
Function getCheckCode ()
{
Return $ this-> checkCode;
}
Private function createImage (){
$ This-> image = imagecreatetruecolor ($ this-> width, $ this-> height );
$ Backcolor = imagecolorallocate ($ this-> image, rand (225,255), rand (225,255), rand (255,255 ));
Imagefill ($ this-> image, 0, 0, $ backcolor );
$ Border = imagecolorallocate ($ this-> image, 0, 0 );
Imagerectangle ($ this-> image, 0, 0, $ this-> width-1, $ this-> height-1, $ border );
}
Private function setDisturbColor (){
For ($ I = 0; $ I <$ this-> disturbColorNum; $ I ++ ){
$ Color = imagecolorallocate ($ this-> image, rand (0,255), rand (0,255), rand (0,255 ));
Imagesetpixel ($ this-> image, rand (1, $ this-> width-2), rand (1, $ this-> height-2), $ color );
}
For ($ I = 0; $ I <10; $ I ++)
{
$ Color = imagecolorallocate ($ this-> image, rand (0,255), rand (0,255), rand (0,255 ));
Imagearc ($ this-> image, rand (-10, $ this-> width), rand (-10, $ this-> height), rand (30,300 ), rand (20,300), $ color );
}
}
Private function outputText ($ fontFace = ""){
For ($ I = 0; $ I <$ this-> codeNum; $ I ++)
{
$ Fontcolor = imagecolorallocate ($ this-> image, rand (0,128), rand (0,128), rand (0,128 ));
If ($ fontFace = "")
{
$ Fontsize = rand (3, 5 );
$ X = floor ($ this-> width/$ this-> codeNum) * $ I + 5;
$ Y = rand (0, $ this-> height-15 );
Imagechar ($ this-> image, $ fontsize, $ x, $ y, $ this-> checkCode {$ I}, $ fontcolor );
}
Else
{
$ Fontsize = rand (12, 16 );
$ X = floor ($ this-> width-8)/$ this-> codeNum) * $ I + 8;
$ Y = rand ($ fontsize, $ this-> height-8 );
Imagettftext ($ this-> image, $ fontsize, rand (-45, 45), $ x, $ y, $ fontcolor, $ fontFace, $ this-> checkCode {$ I });
}
}
}
Private function createCheckCode (){
$ Code = "23456789 abcdefghijkmnpqrstuvwrst ";
$ Str = "";
For ($ I = 0; $ I <$ this-> codeNum; $ I ++)
{
$ Char = $ code {rand (0, strlen ($ code)-1 )};
$ Str. = $ char;
}
Return $ str;
}
Private function outputImage ()
{
If (imagetypes () & IMG_GIF)
{
Header ("Content-Type: image/gif ");
Imagepng ($ this-> image );
} Else if (imagetypes () & IMG_JPG)
{
Header ("Content-Type: image/jpeg ");
Imagepng ($ this-> image );
} Else if (imagetypes () & IMG_PNG)
{
Header ("Content-Type: image/png ");
Imagepng ($ this-> image );
} Else if (imagetypes () & IMG_WBMP ){
Header ("Content-Type: image/vnd. wap. wbmp ");
Imagepng ($ this-> image );
} Else
{
Die ("PHP does not support image Verification Code ");
}
}
// Output the image to the browser using this method
Function showImage ($ fontFace = "")
{
// Create an image background
$ This-> createImage ();
// Sets the interference element.
$ This-> setDisturbColor ();
// Randomly draw text from the image
$ This-> outputText ($ fontFace );
// Output image
$ This-> outputImage ();
}
Function _ destruct ()
{
Imagedestroy ($ this-> image );
}
}
Function checklogin (){
If (empty ($ _ POST ['name'])
Die ('user name cannot be blank ');
If (empty ($ _ POST ['Password'])
Die ("password cannot be blank ");
If ($ _ SESSION ['code']! = $ _ POST ['version'])
Die ("Incorrect verification code input". $ _ SESSION ['code']);
$ Username = $ _ POST ['name'];
$ Password = md5 ($ _ POST ['Password']);
// Check for existence
Conndb ($ username, $ password );
}
Function conndb ($ name = "", $ ps = ""){
$ Conn = mysql_connect ('localhost', 'root', '123 ');
If (! $ Conn) die ("database connection failed". mysql_error ());
Mysql_select_db ('5kan ', $ conn) or die ('database selection failed'. mysql_error ());
Mysql_set_charset ('utf8', $ conn );
$ SQL = "select id from k_user where username = '{$ name}' and password = '{$ ps }'";
$ Result = mysql_query ($ SQL) or die ("SQL statement error". mysql_error ());
If (mysql_num_rows ($ result)> 0) die ("Logon successful ");
Else die ("incorrect user name or password ");
Mysql_close ($ conn );
}
Session_start ();
If (! Isset ($ _ POST ['randnum'])
{
$ Code = new ValidationCode (120,20, 4 );
$ Code-> showImage ("comicbd. ttf"); // displayed on the page
$ _ SESSION ['code'] = $ code-> getCheckCode (); // save it to the server
}
Else
{
Checklogin ();
}
?> |