Php-based digital verification code and digital operation verification code, and php-based digital verification code operation
This article describes the php-implemented digital verification code and the digital operation verification code. Share it with you for your reference. The details are as follows:
1. Digital verification code:
<? Php // The first instance is a digital verification code, the most common verification code. You can decide the number. // $ Num is the generated verification code containing several numbers getValidate (, 20); function getValidate ($ num, $ w, $ h) {$ code = ""; for ($ I = 0; $ I <$ num; $ I ++) {$ code. = rand ();} // The code variable ends with $ num and is a string. If a four-digit verification code is generated, you can use rand (,) to generate the Header ("Content-type: image/PNG"); $ img = imagecreate ($ w, $ h); // create an image. The length and width parameters are defined at the beginning. $ Black = imagecolorallocate ($ img, 200,200,200, 0); // define black $ gray = imagecolorallocate ($ img,); // define gray $ bgcolor = imagecolorallocate ($ img, 255,255,255); // background color white imagefill ($ img, $ gray); // imagefill ($ image, ing $ x, int $ y, int $ color ), fill the image coordinates x and y in the color execution area. // draw a border for the verification code. The verification code is beautiful, in fact, draw a border is to give him a rectangle imagerectangle ($ img, $ W-1, $ h-1, $ black ); // use a black box/* imagestring ($ image, $ font, $ x, $ y, $ s, $ col) to draw the string s X and y of the image (xy is the coordinate of the upper left corner of the string). The upper left corner of the entire image is (). If font is, 5. Use the built-in font * // to draw four numbers, so that the four characters are randomly equal to $ strx = rand (5, 10); for ($ I = 0; $ I <$ num; $ I ++) {$ strops = rand (1, 6); imagestring ($ img, 5, $ strx, $ strops, substr ($ code, $ I, 1), $ black); $ strx + = rand (8, 12); // strx is the abscissa. You need to add the previous coordinates each time to ensure that they do not overlap. Y coordinate no matter} // substr () function returns a part of the string, substr (string, start, length), the string to be truncated, start position, length truncated length // a number is generated. The following section will interfere with the verification code area to Prevent Automatic Identification by some tools. method 1: add a lot of noise to the background // imagesetpixel ($ image, $ x, $ y, $ color), and use color in x in the image, draw a point on the y coordinate (0, 0 in the upper left corner of the image) for ($ I = 0; $ I <80; $ I ++) {imagesetpixel ($ img, rand (0, $ w), rand (0, $ h), $ black); // These vertices are distributed in the background} // 2. method 2: Draw several dotted lines $ style = array ($ black, $ gray); imagesetstyle ($ img, $ style); // imagesetstyl E ($ image, $ style), set the style of the draw line, an array consisting of pixels $ y1 = rand (0, $ h); $ y2 = rand (0, $ h ); $ y3 = rand (0, $ h); $ y4 = rand (0, $ h); imageline ($ img, 0, $ y1, $ w, $ y2, IMG_COLOR_STYLED); imageline ($ img, 0, $ y3, $ w, $ y4, IMG_COLOR_STYLED); imagepng ($ img); imagedestroy ($ img);}?>
2. Digital Operation verification code:
<?phpgetValidate(100,30);function getValidate($w,$h){ $img = imagecreate($w,$h); $gray = imagecolorallocate($img,255,255,255); $black = imagecolorallocate($img,rand(0,200),rand(0,200),rand(0,200)); $red = imagecolorallocate($img, 255, 0, 0); $white = imagecolorallocate($img, 255, 255, 255); $green = imagecolorallocate($img, 0, 255, 0); $blue = imagecolorallocate($img, 0, 0, 255); imagefilledrectangle($img, 0, 0, 100, 30, $black); for($i = 0;$i < 80;$i++){ imagesetpixel($img, rand(0,$w), rand(0,$h), $gray); } $num1 = rand(1,99); $num2 = rand(1,99); imagestring($img, 5, 5, rand(1,10), $num1, $red); imagestring($img,5,30,rand(1,10),getRand(), $white); imagestring($img,5,45,rand(1,10),$num2, $green); imagestring($img,5,65,rand(1,10),"=", $blue); imagestring($img,5,80,rand(1,10),"?", $red); header("content-type:image/png"); imagepng($img); imagedestroy($img);}function getRand(){ $code = rand(0,1); switch ($code) { case 0: return "+"; break; case 1: return "-"; break; default: # code... break; }}?>
I hope this article will help you with php programming.