Home > Backend Development > PHP Tutorial > 验证码图片不显示解决方法

验证码图片不显示解决方法

WBOY
Release: 2016-06-13 12:14:47
Original
3139 people have browsed it

验证码图片不显示
本人不会PHP,所以还请各位大神见谅
直接访问这个页面也不显示(红色XX)
本人百度试着解决
GD是开着的
可还是不显示图片
求大神帮助

<br /><?php<br />ini_set('display_errors', 'Off');<br />Header("Content-type: image/gif");<br />/*<br />* 初始化<br />*/<br />$border = 0; //是否要边框 1要:0不要<br />$how = 4; //验证码位数<br />$w = $how*15; //图片宽度<br />$h = 20; //图片高度<br />$fontsize = 5; //字体大小<br />$alpha = "abcdefghijkmnopqrstuvwxyz"; //验证码内容1:字母<br />$number = "023456789"; //验证码内容2:数字<br />$randcode = ""; //验证码字符串初始化<br />srand((double)microtime()*1000000); //初始化随机数种子	<br /><br />$im = ImageCreate($w, $h); //创建验证图片<br /><br />/*<br />* 绘制基本框架<br />*/<br />$bgcolor = ImageColorAllocate($im, 255, 255, 255); //设置背景颜色<br />ImageFill($im, 0, 0, $bgcolor); //填充背景色<br />if($border)<br />{<br />    $black = ImageColorAllocate($im, 0, 0, 0); //设置边框颜色<br />    ImageRectangle($im, 0, 0, $w-1, $h-1, $black);//绘制边框<br />}<br /><br />/*<br />* 逐位产生随机字符<br />*/<br />for($i=0; $i<$how; $i++)<br />{   <br />    $alpha_or_number = mt_rand(0, 1); //字母还是数字<br />    $str = $alpha_or_number ? $alpha : $number;<br />    $which = mt_rand(0, strlen($str)-1); //取哪个字符<br />    $code = substr($str, $which, 1); //取字符<br />    $j = !$i ? 4 : $j+15; //绘字符位置<br />    $color3 = ImageColorAllocate($im, mt_rand(0,100), mt_rand(0,100), mt_rand(0,100)); //字符随即颜色<br />    ImageChar($im, $fontsize, $j, 3, $code, $color3); //绘字符<br />    $randcode .= $code; //逐位加入验证码字符串<br />}<br /><br />/*<br />* 添加干扰<br />*/<br />for($i=0; $i<5; $i++)//绘背景干扰线<br />{   <br />    $color1 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); //干扰线颜色<br />    ImageArc($im, mt_rand(-5,$w), mt_rand(-5,$h), mt_rand(20,300), mt_rand(20,200), 55, 44, $color1); //干扰线<br />}   <br />for($i=0; $i<$how*40; $i++)//绘背景干扰点<br />{   <br />    $color2 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); //干扰点颜色 <br />    ImageSetPixel($im, mt_rand(0,$w), mt_rand(0,$h), $color2); //干扰点<br />}<br /><br />//把验证码字符串写入session<br />session_start();<br />$_SESSION["usimga"] = $randcode;<br /><br />/*绘图结束*/<br />Imagegif($im);<br />ImageDestroy($im);<br />/*绘图结束*/<br />?> <br /><br />
Copy after login

------解决思路----------------------
代码本身没有问题
请检查是否有 BOM 头
------解决思路----------------------


Notice:  A session had already been started - ignoring session_start() in D:\web\legou\usimg.php on line 54


用notepad打开你的图片是这样的。
很明显,是session_start()之前有其他输出或者空白。

去掉空白后,图片是正常的:

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template