Verification codes are very important in WEB applications. they are usually used to prevent users from submitting forms maliciously, such as malicious registration and login, and malicious Forum bumping. This article describes how to use PHP to generate common verification codes.
Then, paste the code directly.
<? Php $ image = imagecreatetruecolor (100, 30); // Create a canvas $ imagecolor = imagecolorallocate ($ image, 255,255,255); // background color imagefill ($ image, 0, 0, $ imagecolor); // fill in the background color for ($ I = 0; $ I <4; $ I ++) {// Loop 4 digits $ fontsize = 6; $ fontcolor = imagecolorallocate ($ images, rand (0,200), rand (0,200), rand (0,200); $ fontcontent = rand (0, 9 ); $ x = $ I * 100/4 + rand (5, 15); $ y = rand (5, 10); imagestring ($ image, $ fontsize, $ x, $ y, $ Fontcontent, $ fontcolor);} for ($ I = 0; $ I <200; $ I ++) {// add interference points cyclically $ pointcolor = imagecolorallocate ($ image, rand (50,200), rand (50,200), rand (50,200); $ x = rand (1, 99); $ y = rand (1, 29 ); imagesetpixel ($ image, $ x, $ y, $ pointcolor) ;}for ($ I = 0; $ I <3; $ I ++) {// add interference lines cyclically $ linecolor = imagecolorallocate ($ image, rand (100,250), rand (100,250), rand (100,250); $ x1 = rand (1, 25); $ x2 = rand (50, 75); $ y1 = r And (1, 15); $ y2 = rand (15, 25); imageline ($ image, $ x1, $ y1, $ x2, $ y2, $ linecolor );} header ("content-type: image/png"); imagepng ($ image); imagedestroy ($ image);?>
I will share with you a Chinese verification code that can be generated.
<? Php // 1. qi enables the gd library of the GD library to provide a series of APIs for image processing. the GD library can be used to process images or generate images. // The GD library on the website is usually used to generate thumbnails, add watermarks to images, or generate reports on website data. Session_start (); // converts a GBK encoded string to a UTF-8 string. The first parameter is written in GBK, is because the php file stored in the host encoding is GBK encoding // UTF-8 encoding browser universal support, versatility, here is converted to UTF-8 $ str = iconv ("GBK ", "UTF-8", "all sentient beings, green mountains, scenic spots, and monuments are open to us."); if (! Is_string ($ str) |! Mb_check_encoding ($ str, "UTF-8") {exit ("not a string or UTF-8");} $ zhongwenku_size; // get the length of the string by UTF-8 encoding $ zhongwenku_size = mb_strlen ($ str, "UTF-8"); // import the above character into the array $ zhongwenku = array (); for ($ I = 0; $ I <$ zhongwenku_size; $ I ++) {$ zhongwenku [$ I] = mb_substr ($ str, $ I, 1, "UTF-8") ;}$ result = ""; // four characters to write to the image for ($ I = 0; $ I <4; $ I ++) {switch (rand (0, 1) {case 0: $ result. = $ zhongwenku [rand (0, $ zhongwenku_size-1)]; break; case 1: $ r Esult. = dechex (rand (100); break ;}$ _ SESSION ["check"] = $ result; // create a true color image with a width, height 30 $ img = imagecreatetruecolor (100, 30); // assign the background color $ bg = imagecolorallocate ($ img, 0, 0, 0 ); // assign the text color $ te = imagecolorallocate ($ img, 255,255,255); // write the string on the image // imagestring ($ img, rand (), rand ), rand (), $ result, $ te); // you can write special fonts imagettftext ($ img, 13, rand (2, 9), 20, 20, $ te, "MSYH. TTF ", $ result); $ _ SESSION ["Check"] = $ result; for ($ I = 0; $ I <3; $ I ++) {// $ t = imagecolorallocate ($ img, rand (0,255), rand (0,255), rand (0,255); // draw line imageline ($ img, 0, rand (0, 20), rand (70,100 ), rand (0, 20), $ te) ;}$ t = imagecolorallocate ($ img, rand (0,255), rand (0,255), rand (0,255 )); // add noise for ($ I = 0; $ I <200; $ I ++) {imagesetpixel ($ img, rand (1,100), rand (1, 30), $ t) ;}// send http header information to specify that the sender header ("Content-type: image/ Jpeg "); // output the jpeg image to the browser imagejpeg ($ img);?>
Let's try another instance.
<? Php session_start (); function random ($ len) {$ srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm"; mt_srand (); $ strs = ""; for ($ I = 0; $ I <$ len; $ I ++) {$ strs. = $ srcstr [mt_rand (0, 30)];} return $ strs;} // randomly generated string $ str = random (4 ); // the width of the verification code Image $ width = 50; // The height of the verification code Image $ height = 25; // declare the image format of the layer to be created @ header ("Content-Type: image/png "); // create a layer $ im = imagecreate ($ width, $ height); // background color $ back = imagecolorallocate ($ I M, 0xFF, 0xFF, 0xFF); // blurred dot color $ pix = imagecolorallocate ($ im, 187,230,247); // font color $ font = imagecolorallocate ($ im, 41,163,238 ); // plot the blur point mt_srand (); for ($ I = 0; $ I <1000; $ I ++) {imagesetpixel ($ im, mt_rand (0, $ width), mt_rand (0, $ height), $ pix);} // output character imagestring ($ im, 5, 7, 5, $ str, $ font ); // output rectangular imagerectangle ($ im, 0, 0, $ width-1, $ height-1, $ font); // output image imagepng ($ im ); imagedestroy ($ im); $ st R = md5 ($ str); // select cookie // SetCookie ("verification", $ str, time () + 7200 ,"/"); // select Session $ _ SESSION ["verification"] = $ str;?>
Next, you just need to call it on the page:
If you want to achieve "cannot see clearly? For a different effect, add the following JS to the page.
function changing(){ document.getElementById('checkpic').src="/images/checkcode.php?"+Math.random();}
The above is all the content of this article. I hope you will like it.