This article mainly introduces the digital verification code and the digital operation Verification Code implemented by php. It describes the related techniques for php to implement the digital verification code and the mathematical operation verification code in the form of examples, which is very simple and practical, for more information, see
This article mainly introduces the digital verification code and the digital operation Verification Code implemented by php. It describes the related techniques for php to implement the digital verification code and the mathematical operation verification code in the form of examples, which is very simple and practical, for more information, see
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); // imagesetstyle ($ 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 (255,255,255); function getValidate ($ w, $ h) {$ img = imagecreate ($ w, $ h); $ gray = imagecolorallocate ($ img ); $ 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 (), $ num1, $ red); imagestring ($ img, rand (), getRand (), $ white); imagestring ($ img, rand (), $ num2, $ green); imagestring ($ img, rand (), "= ", $ blue); imagestring ($ img, rand ),"? ", $ 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.
,