In this paper, we demonstrate 5 kinds of verification codes, and describe the function of generating verification code. PHP generated Verification code principle: Through the GD library, generate a picture with a verification code, and save the Verification code in the session.
Session_Start (); GetCode (4,60,20); function GetCode ($num, $w, $h) {$code = ""; for ($i = 0; $i < $num; $i + +) {$code. = rand (0, 9); }//4-bit verification code can also be generated directly with Rand (1000,9999)//The generated verification code is written to the SESSION, the verification with $_session["helloweba_num"] = $code; Create a picture, define the color value header ("Content-type:image/png"); $im = Imagecreate ($w, $h); $black = imagecolorallocate ($im, 0, 0, 0); $gray = Imagecolorallocate ($im, 200, 200, 200); $bgcolor = Imagecolorallocate ($im, 255, 255, 255); Fill Background imagefill ($im, 0, 0, $gray); Draw Border Imagerectangle ($im, 0, 0, $w-1, $h-1, $black); Randomly draws two dashed lines, acts as a disturbance $style = Array ($black, $black, $black, $black, $black, $gray, $gray, $gray, $gray, $gray); Imagesetstyle ($im, $style); $y 1 = rand (0, $h); $y 2 = rand (0, $h); $y 3 = rand (0, $h); $y 4 = rand (0, $h); Imageline ($im, 0, $y 1, $w, $y 3, img_color_styled); Imageline ($im, 0, $y 2, $w, $y 4, img_color_styled); A large number of black dots are randomly generated on the canvas, which acts as interference; for ($i = 0; $i < $i + +) {Imagesetpixel ($im, rand (0,$W), rand (0, $h), $black); The numbers are randomly displayed on the canvas, and the horizontal spacing and position of the characters are randomly generated according to a certain range of fluctuations $strx = rand (3, 8); for ($i = 0; $i < $num; $i + +) {$strpos = rand (1, 6); Imagestring ($im, 5, $strx, $strpos, substr ($code, $i, 1), $black); $strx + = rand (8, 12); } imagepng ($im);//Output Picture Imagedestroy ($im);//release Image Memory}
The above mentioned is the whole content of this article, I hope you can like.