Ec (2); & nbsp; php Verification Code Program & nbsp; Verification Code phpphp image Verification Code & lt ;? Php * @ date & nbsp; 20080514 @ author & nbsp; & nbsp; hlua script ec (2); script
Php Verification Code ProgramVerification Code phpPhp image Verification Code
/*
@ Date 20080514
@ Author hluan
// Session_start ();
Class icode
{
Function _ construct (){
Header ('content-Type: image/png ');
}
Function _ destruct (){
Imagedestroy ($ png );
}
Function getcode ($ len, $ type ){
Switch ($ type ){
Case 1;
$ Str = "1234567890 ";
Break;
Case 2;
$ Str = "abcdefghijklmnopqrstuvwxyz ";
Break;
Case 0;
$ Str = "1234567890 abcdefghijklmnopqrstuvwxyz ";
Break;
}
$ Result = "";
$ Length = strlen ($ str)-1;
$ Num = 0;
For ($ I = 0; $ I <$ len; $ I ++ ){
$ Num = rand (0, $ length );
$ A = $ str [$ num];
$ Result = $ result. $;
}
/* Before using it, you shoshould use session_start ()*/
$ _ SESSION ['code'] = $ result; // Store in session.
// Echo "session:". $ _ SESSION ['icode'];
// Die ();
$ Png = imagecreate (60, 30 );
$ White = imagecolorallocate ($ png, 255,255,255); // background
$ Red = imagecolorallocate ($ png, 255, 0, 0 );
$ Blue = imagecolorallocate ($ png, 255 );
$ Brown = imagecolorallocate ($ png, 100,0, 0 );
$ Black = imagecolorallocate ($ png, 0, 0); // Identifying Code
Imagefill ($ png, 0, 0, $ white );
$ K = rand (0, 3 );
If (0 = $ k ){
For ($ n = 0; $ n <60; $ n ++ ){
$ Y = 15 * sin ($ n/30 * pi ());
Imagesetpixel ($ png, $ n, 15 + $ y, $ red );
}
} Else if (1 ==$ k ){
For ($ n = 0; $ n <60; $ n ++ ){
$ Y = 15 * cos ($ n/60 * pi ());
Imagesetpixel ($ png, $ n, 15 + $ y, $ red );
}
} Else if (2 = $ k ){
For ($ n = 0; $ n <60; $ n ++ ){
$ Y = 15 * sin ($ n/45 * pi ());
Imagesetpixel ($ png, $ n, 15 + $ y, $ brown );
}
} Else if (3 ==$ k ){
For ($ n = 0; $ n <60; $ n ++ ){
$ Y = 15 * cos ($ n/30 * pi ());
Imagesetpixel ($ png, $ n, 15 + $ y, $ brown );
}
}
For ($ k = 0; $ k <10; $ k ++ ){
$ I = rand (3,60); // width
$ J = rand (3, 15); // height
Imageline ($ png, $ I-3, $ J-3, $ I, $ j, $ black );
}
Imagestring ($ png, 5, 3, 5, $ result, $ blue );
$ Icode = imagepng ($ png );
Return $ icode;
}
}
/* Test code
$ Icode = new icode ();
$ Icp = $ icode-> getcode (6, 0 );
Echo $ icp;
*/