Share a PHP verification code generation class written by a person, and share a php verification code generation class. For personal PHP verification code generation class sharing, php verification code generation and sharing this verification code class can be used directly, you can also refer! Among them, the class member codestr is the generated verification code string, and the PHP verification code generation class is shared.
This verification code class can be used directly, or you can refer to it!
Among them, the class member codestr is the generated verification code string:
<? Php/*** verification Code */class Code {// 1. define the width, height, canvas, word count, type, and painting type of each member: private $ width; // width: private $ height; // height: private $ num; // verification code word count private $ imgType; // Generate image Type private $ Type; // string Type 1, 2, 3 Three Options 1 pure number 2 pure lowercase letters 3 uppercase and lowercase numbers mix private $ hb; // canvas public $ codestr; // verification code string public function _ construct ($ height = 20, $ num = 4, $ imgType = "jpeg", $ Type = 1) {$ this-> width = $ num * 20; $ this-> height = $ height; $ this-> num = $ num; $ this-> img Type = $ imgType; $ this-> Type = $ Type; $ this-> codestr = $ this-> codestr (); $ this-> zuhe ();} // 2. define the random string acquisition function private function codestr () {switch ($ this-> Type) {case 1: // Type 1 get a random number 1-9 $ str = implode ("", array_rand (range (), $ this-> num); break; case 2: // type 2 get a-z random lowercase letter $ str = implode ("", array_rand (array_flip (range (a, z), $ this-> num); break; case 3: // type 3 to get numbers, lowercase letters, uppercase letters mixed for ($ I = 0; $ I <$ this-> num; $ I ++) {$ m = rand (0, 2); switch ($ m) {case 0: $ o = rand (48, 57); break; case 1: $ o = rand (97,122); break; case 2: $ o = rand (); break;} $ str. = sprintf ("% c", $ o);} break;} return $ str;} // 3. initialize the canvas image resource private function Hb () {$ this-> hb = imagecreatetruecolor ($ this-> width, $ this-> height);} // 4. generate background color private function Bg () {return imagecolorallocate ($ this-> hb, rand (130,250), rand (130,250), rand (13 0,250);} // 5. generate Font color private function Font () {return imagecolorallocate ($ this-> hb, rand (0,100), rand (0,100), rand (0,100);} // 6. fill background color private function BgColor () {imagefilledrectangle ($ this-> hb, $ this-> width, $ this-> height, $ this-> Bg ();} // 7. interference point private function ganrao () {$ sum = floor ($ this-> width) * ($ this-> height)/3); for ($ I = 0; $ I <$ sum; $ I ++) {imagesetpixel ($ this-> hb, rand (0, $ this-> width), rand (0, $ this-> height), $ this-> Bg () ;}// 8. random straight line arc private function huxian () {for ($ I = 0; $ I <$ this-> num; $ I ++) {imageArc ($ this-> hb, rand (0, $ this-> width), rand (0, $ this-> height), rand (0, $ this-> width), rand (0, $ this-> height), rand (0,360), rand (0,360), $ this-> Bg () ;}// 9. write private function xiezi () {for ($ I = 0; $ I <$ this-> num; $ I ++) {$ x = ceil ($ this-> width/$ this-> num) * $ I; $ y = rand (1, $ this-> height-15 ); imagechar ($ this -> Hb, 5, $ x + 4, $ y, $ this-> codestr [$ I], $ this-> Font () ;}// 10. output private function OutImg () {$ shuchu = "image ". $ this-> imgType; $ header = "Content-type: image /". $ this-> imgType; if (function_exists ($ shuchu) {header ($ header); $ shuchu ($ this-> hb );} else {exit ("GD library does not have such images") ;}// 11. assemble private function zuhe () {$ this-> Hb (); $ this-> BgColor (); $ this-> ganrao (); $ this-> huxian (); $ this-> xiezi (); $ this-> OutImg ();} Public function getCodeStr () {return $ this-> codestr ;}}?>
PHP cannot generate images when generating verification codes. they are all default images marked with images with codes in the GD Library.
Share a verification code experience: when using php to generate a verification code that cannot be normally displayed, you should first open the php script that generates the verification code in the browser to check it, if some information similar to "image error" is displayed, it means that your php program is not running normally. then, you can set "header (" content-type: image/png "); "remove this sentence and refresh the page. you can locate the problem and solve it step by step based on the error message. no problem at last, and then put "header... "add that sentence, and then look at it again.
"Header (" content-type: image/png ");" tells the browser that "I" is an image. with this sentence, the browser will not display the php error normally.
Which of the following code writes a random verification code using php?
Here is the PHP verification code class.
Www.phpha.com/archives/31.html
Refer this verification code class can be used directly, you can also refer! Among them, the class member codestr is the generated verification code string...