Today, I used code to implement the PHP verification code generation function, and found that firefox always prompts "image ....... due to its own error and inability to display, the author also prompted that "if the browser displays" image XXX cannot be displayed due to its own error ", try to remove spaces in the text ", however, if all spaces in the Code are removed, the Inspection image cannot be displayed. As a result, the depth of google, most of the solutions will also be "<?" Before this code, press enter with a space to delete everything to prevent html output. However, this does not solve my problem. Later I saw someone in Header ("Content-type: image/PNG "); Use ob_clean () to clear the output before the code. The result is successful. Modify the original code and paste it below for future use (see the original post for usage ):
CheckNum_session.php
Header ("Content-type: image/png");/** initialization */$ border = 1; // whether to set border 1 to: 0 not $ how = 4; // number of digits of the Verification Code $ w = $ how * 15; // Image Width $ h = 20; // Image Height $ fontsize = 6; // font size $ alpha = "abcdefghijkmnopqrstuvwxyz"; // Verification Code content 1: letter $ number = "023456789"; // Verification Code content 2: number $ randcode = ""; // The verification code string initializes srand (double) microtime () * 1000000); // initializes the random number seed $ im = ImageCreate ($ w, $ h ); // create a verification image/** draw basic framework */$ bgcolor = ImageColorAllocate ($ im, 255,255,255); // set Background Color ImageFill ($ im, 0, 0, $ bgcolor); // fill in the background color if ($ border) {$ black = ImageColorAllocate ($ im, 0, 0, 0 ); // set the border color ImageRectangle ($ im, 0, 0, $ W-1, $ h-1, $ black ); // draw borders}/** generate random characters by bit */for ($ I = 0; $ I <$ how; $ I ++) {$ alpha_or_number = mt_rand (0, 1); // a letter or number $ str = $ alpha_or_number? $ Alpha: $ number; $ which = mt_rand (0, strlen ($ str)-1); // specify the character $ code = substr ($ str, $ which, 1); // get the character $ j =! $ I? 4: $ j + 15; // The Position of the painted character $ color3 = ImageColorAllocate ($ im, mt_rand (0,100), mt_rand (0,100), mt_rand (0,100 )); // The Color of the character ImageChar ($ im, $ fontsize, $ j, 3, $ code, $ color3); // draw the character $ randcode. = $ code; // Add the verification code string in a bit by bit} // write the verification code string to sessionsession_start (); $ _ SESSION ['authnum _ session'] = $ randcode; /** add interference */for ($ I = 0; $ I <1; $ I ++) // draw the background interference line {$ color1 = ImageColorAllocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255); // interference line color ImageArc ($ im, mt_rand (-5, $ w), mt_rand (-5, $ h), mt_rand (20,300), mt_rand (20,200), 55, 44, $ color1); // interference line} for ($ I = 0; $ I <$ how * 40; $ I ++) // draws background interference points {$ color2 = ImageColorAllocate ($ im, mt_rand (0,255), mt_rand (0,255 ), mt_rand (0,255); // The disturbance point color ImageSetPixel ($ im, mt_rand (0, $ w), mt_rand (0, $ h), $ color2 ); // interference point} // key code to prevent the problem 'image cannot be displayed due to its own mistake '. ob_clean ();/* drawing ends */Imagegif ($ im ); imageDestroy ($ im);/* draw end */
The confirmation code calls the sessionValidate. php file.
<? Phpsession_start (); // enable the session first on the top, // error_reporting (2047); session_destroy (); // remove the session to get the new session value every time; // The effect of using seesion is good and convenient?> <Html>
From: http://ddkangfu.blog.51cto.com/311989/608203