Why is ob_clean required to display the verification code in php?
Ob_clean (); header ("content-type: image/jpeg"); // Generate the verification code $ char = '1234567890abcdefghijklmnopqrstuvwxyz '; $ len = 4; $ schar = ''; $ charlen = strlen ($ char); for ($ I = 0; $ I <$ len; $ I ++) {$ schar. = $ char [rand (0, $ charlen)];} // Save the result to the session @ session_start (); $ _ SESSION ['captcha _ Code'] = $ schar; // Read Image $ bg_file = '. /captcha/captcha_bg '. mt_rand (1, 5 ). '.jpg '; // Create a canvas based on the image $ img = imagecreatefromjpeg ($ bg_file); if (rand (1, 2) = 1) {$ color = imagecolorallocate ($ img, 0, 0, 0);} else {$ color = imagecolorallocate ($ img, 255,255,255);} $ imgsize = getimagesize ($ bg_file); imagestring ($ img, 5, 30, 0, $ schar, $ color); imagepng ($ img); imagedestroy ($ img );
I am a newbie who just started learning the verification code. if ob_clean () is not enabled, it is an X. The GD library of the configuration file has been enabled and apache is restarted. I hope to know the instructions and explain the reasons.
Reply to discussion (solution)
First
Ob_clean ();
Switch to the following format to see if there is any output
$data = ob_get_contents();ob_clean();var_dump($data);
This indicates that there is output in front of your program,
This indicates that there is output in front of your program,
Thank you for solving the problem. it is true that you have added blank lines to the front.