Home >Backend Development >PHP Tutorial >php中 为什么验证码 必须要开启 ob_clean 才可以显示

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

WBOY
WBOYOriginal
2016-06-23 13:45:40998browse

ob_clean();header("content-type: image/jpeg");//生成验证码$char = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';$len = 4;$schar = '';$charlen = strlen($char);for ($i=0; $i < $len; $i++) {     $schar .= $char[rand(0,$charlen)];}//将结果保存到 session中@session_start();$_SESSION['captcha_code'] = $schar;//读取图片$bg_file = './captcha/captcha_bg' . mt_rand(1,5) . '.jpg';//根据图片创建画布$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);


本人新手 刚开始学习, 验证码如果不开启ob_clean(); 就是一个  X  , 配置文件 GD 库已经开启并且重启 apache.  希望知道的指导一下并且说明原因.


回复讨论(解决方案)

先把
ob_clean(); 
换成下面的形式   然后看看有没有输出

$data = ob_get_contents();ob_clean();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
Previous article:sql关联问题ABC三个表,Next article::PHP代码排错