Click php to refresh the verification code, and php to refresh the verification code. Php click to refresh the verification code, php implements refresh the verification code file CreateImg. class. phpphpclassValidationCode {private $ width, $ height, $ codenum; public $ checkcode; click php to refresh the verification code, and php to refresh the verification code.
Verification code file CreateImg. class. php
<? Php class ValidationCode {private $ width, $ height, $ codenum; public $ checkcode; // generated verification code private $ checkimage; // verification code Image private $ disturbColor = ''; // interfere with the Pixel function _ construct ($ width = '80', $ height = '20', $ codenum = '4') {$ this-> width = $ width; $ this-> height = $ height; $ this-> codenum = $ codenum;} function outImg () {// output header $ this-> outFileHeader (); // Generate a verification code $ this-> createCode (); // generate an image $ this-> createImage (); // Set the interference pixel $ This-> setDisturbColor (); // write the verification code to the image $ this-> writeCheckCodeToImage (); imagepng ($ this-> checkimage ); imagedestroy ($ this-> checkimage);} private function outFileHeader () {header ("Content-type: image/png");} private function createCode () {$ this-> checkcode = strtoupper (substr (md5 (rand (), 0, $ this-> codenum);} private function createImage () {$ this-> checkimage = @ imagecreate ($ this-> width, $ this-> heig Ht); $ back = imagecolorallocate ($ this-> checkimage, 255,255,255); $ border = imagecolorallocate ($ this-> checkimage, 0 ); imagefilledrectangle ($ this-> checkimage, 0, 0, $ this-> width-1, $ this-> height-1, $ back ); // white-colored imagerectangle ($ this-> checkimage, 0, 0, $ this-> width-1, $ this-> height-1, $ border ); // black border} private function setDisturbColor () {for ($ I = 0; $ I <= 200; $ I ++) {$ this-> disturbColor = imagecolora Llocate ($ this-> checkimage, rand (0,255), rand (0,255), rand (0,255); imagesetpixel ($ this-> checkimage, rand (2,128 ), rand (2,38), $ this-> disturbColor) ;}} private function writeCheckCodeToImage () {for ($ I = 0; $ I <= $ this-> codenum; $ I ++) {$ bg_color = imagecolorallocate ($ this-> checkimage, rand (0,255), rand (0,128), rand (0,255 )); $ x = floor ($ this-> width/$ this-> codenum) * $ I; $ y = rand (0, $ this-> height-15); imagec Har ($ this-> checkimage, rand (5, 8), $ x, $ y, $ this-> checkcode [$ I], $ bg_color );}} function _ destruct () {unset ($ this-> width, $ this-> height, $ this-> codenum) ;}}?>
Contains the file imgcode. php
<? Php session_start (); require_once ('createimg. class. php '); $ image = new ValidationCode ('80', '20', '4'); // image length, width, and number of characters $ image-> outImg (); $ _ SESSION ['validationcode'] = $ image-> checkcode; // store the verification code to $ _ SESSION?>
Front-end file demo. php
? Php session_start (); $ test =$ _ POST ['test']; $ test = strtoupper (trim ($ test )); $ submit = $ _ POST ['submit ']; if (isset ($ submit) {if ($ test = $ _ SESSION ['validationcode']) {echo 'true';} else {echo 'false'; }}?> Image