Home > Article > Backend Development > php中 为何验证码 必须要开启 ob_clean 才可以显示
php中 为什么验证码 必须要开启 ob_clean 才可以显示
<br />ob_clean();<br />header("content-type: image/jpeg");<br /><br />//生成验证码<br />$char = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';<br />$len = 4;<br />$schar = '';<br />$charlen = strlen($char);<br /><br />for ($i=0; $i < $len; $i++) { <br /> $schar .= $char[rand(0,$charlen)];<br />}<br /><br />//将结果保存到 session中<br />@session_start();<br />$_SESSION['captcha_code'] = $schar;<br /><br /><br />//读取图片<br />$bg_file = './captcha/captcha_bg' . mt_rand(1,5) . '.jpg';<br />//根据图片创建画布<br />$img = imagecreatefromjpeg($bg_file);<br /><br />if (rand(1,2) == 1) {<br /> $color = imagecolorallocate($img, 0, 0, 0 );<br />}else {<br /> $color = imagecolorallocate($img, 255, 255, 255);<br />}<br /><br />$imgsize = getimagesize($bg_file);<br /><br /><br /><br />imagestring($img, 5, 30, 0, $schar,$color );<br /><br />imagepng($img);<br /><br />imagedestroy($img);<br />
$data = ob_get_contents();<br />ob_clean();<br />var_dump($data);