Home  >  Article  >  Backend Development  >  php中 为何验证码 必须要开启 ob_clean 才可以显示

php中 为何验证码 必须要开启 ob_clean 才可以显示

WBOY
WBOYOriginal
2016-06-13 12:09:461078browse

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 />


本人新手 刚开始学习, 验证码如果不开启ob_clean(); 就是一个  X  , 配置文件 GD 库已经开启并且重启 apache.  希望知道的指导一下并且说明原因.
------解决思路----------------------
先把
ob_clean(); 
换成下面的形式   然后看看有没有输出
$data = ob_get_contents();<br />ob_clean();<br />var_dump($data);

------解决思路----------------------
这表示你的程序前面有输出,

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn