This article mainly introduces the login verification code function written in PHP. the call method is provided at the end of this article, which includes random character functions and GD Library drawing functions, interested friends can refer to this example to share a login verification code function written in PHP for your reference. The details are as follows:
ShowKey. php
<? Phpsession_start (); // Set COOKIE or Sessionfunction esetcookie ($ name, $ str, $ life = 0) {// This function reduces all str strings and writes the string so that the verification code input is case-insensitive-the same function conversion is required for session comparison in the submission form $ _ SESSION [$ name] = strtolower ($ str );} // obtain random characters. This function is case sensitive. if it is case insensitive, add the strtolowerfunction domake_password ($ len) {$ chars = array (/* "a", "B ", "c", "d", "e", "f", "g", "h", "I", "j", "k", "l ", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v ", "w", "x", "y", "z "," A "," B "," C "," D "," E "," F "," G "," H "," I "," J ", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T ", "U", "V", "W", "X", "Y", "Z", */"0", "1", "2 ", "3", "4", "5", "6", "7", "8", "9"); $ charsLen = count ($ chars)-1; shuffle ($ chars); // disrupt the array $ output = ""; for ($ I = 0; $ I <$ len; $ I ++) {$ output. = $ chars [mt_rand (0, $ charsLen)]; // Get an array element} return $ output;} // display the verification code function ShowKey () {$ key = domake_password (4); // Get the random value $ set = esetcookie ("checkkey", $ key ); // write random values into cookies or sessions // whether the gd library if (function_exists ("imagejpeg") {header ("Content-type: image/jpeg") is supported "); $ img = imagecreate (102,102,102); $ blue = imagecolorallocate ($ img, 255,255,255); $ white = ImageColorAllocate ($ img,); $ black = ImageColorAllocate ($ img, 71); imagefill ($ img, 0, 0, $ blue); imagestring ($ img, 5, 6, 3, $ key, $ white); for ($ I = 0; $ I <90; $ I ++) // add interference pixels {Imagesetpixel ($ img, rand () % 70, rand () % 30, $ black);} imagejpeg ($ img); imagedestroy ($ img );} elseif (function_exists ("imagepng") {header ("Content-type: image/png"); $ img = imagecreate (47,20); $ blue = imagecolorallocate ($ img, 102,102,102); $ white = ImageColorAllocate ($ img, 255,255,255); $ black = ImageColorAllocate ($ img, 71); imagefill ($ img, $ blue ); imagestring ($ img, 5, 6, 3, $ key, $ white); for ($ I = 0; $ I <90; $ I ++) // add interference pixels {imagesetpixel ($ img, rand () % 70, rand () % 30, $ black);} imagepng ($ img ); imagedestroy ($ img);} elseif (function_exists ("imagegif") {header ("Content-type: image/gif"); $ img = imagecreate (47,20 ); $ blue = imagecolorallocate ($ img, 102,102,102); $ white = ImageColorAllocate ($ img, 255,255,255); $ black = ImageColorAllocate ($ img, 71,71, 71); imagefill ($ img, 0, 0, $ blue); imagestring ($ img, 5, 6, 3, $ key, $ white ); For ($ I = 0; $ I <90; $ I ++) // add interference pixels {imagesetpixel ($ img, rand () % 70, rand () % 30, $ black);} imagegif ($ img); imagedestroy ($ img);} elseif (function_exists ("imagewbmp") {header ("Content-type: image/vnd. wap. wbmp "); $ img = imagecreate (102,102,102); $ blue = imagecolorallocate ($ img, 255,255,255); $ white = ImageColorAllocate ($ img ); $ black = ImageColorAllocate ($ img, 71); imagefill ($ img, $ blue); imagestring ($ Img, 5, 6, 3, $ key, $ white); for ($ I = 0; $ I <90; $ I ++) // add interference pixels {imagesetpixel ($ img, rand () % 70, rand () % 30, $ black);} imagewbmp ($ img ); imagedestroy ($ img);} else {// verification code header ("content-type: image/jpeg \ r \ n"); header ("Pragma: no-cache \ r \ n "); header (" Cache-Control: no-cache \ r \ n "); header (" Expires: 0 \ r \ n "); $ fp = fopen ("data/vdcode.jpg", "r") ;}} ShowKey () ;?>
Call method:
The code is as follows:
The above is all the content of this article. I hope it will help you learn php programming.