code.php 这个文件<?php 
session_start();
//定义个常量,用来授权调用includes里面的文件
define('IN_TG',true);
//引入公共文件
require dirname(__FILE__).'/includes/common.inc.php'; //此文件中引用了 global.func.php 文件
//调用方法生成验证码
_code();-----------------------------以下是global.func.php文件-------------------------------------------------------- /**
 * _code()是验证码函数
 * @access public 
 * @param int $width 表示验证码的长度
 * @param int $height 表示验证码的高度
 * @param int $_rnd_code 表示验证码的位数
 * @param bool $_flag 表示验证码是否需要边框 
 * @return void 这个函数执行后产生一个验证码
 */
 function _code($width=75,$height=25,$_rnd_code=4,$_flag=false)
 {
 
 //随机码的个数
 $_rnd_code=4;
 
 //随机产生验证码
 $_nmsg="";
 for($i=0;$i<$_rnd_code;$i++)
 {
  $_nmsg .=dechex(mt_rand(0,15));
 }
 
 //将验证码保存在session中
 $_SESSION['code']=$_nmsg;
 
 
 //创建一张图像
 $image=imagecreatetruecolor($width,$height);//背景颜色
 $_white=imagecolorallocate($image,255,255,255);
 
 //填充颜色
 imagefill($image,0,0,$_white);
 
 
 //创建黑色边框
 if($_flag)
 {  
  $_black=imagecolorallocate($image,0,0,0);
  imagerectangle($image,0,0,$width-1,$height-1,$_black);
 } //随机画6个线条
 for($i=0;$i<6;$i++)
 {
  $_line_color=imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  imageline($image,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),$_line_color);
 }
 
 //随机雪花
 for($i=0;$i<100;$i++)
 {
  $_snow_color=imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  imagestring($image,1,mt_rand(1,$width),mt_rand(1,$height),'*',$_snow_color);
 }
 
 //输出验证码
 for($i=0;$i<strlen($_SESSION['code']);$i++)
 {
 $_out_color=imagecolorallocate($image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));
 
 imagestring($image,5,$width*$i/$_rnd_code+mt_rand(0,10),mt_rand(1,$height/2),$_SESSION['code'][$i],$_out_color);
 }//输出图像
 header("Content-Type:image/png");
 imagepng($image);
 
 //销毁
imagedestroy($image);
 
 }----------------------------------产生错误如下-----------------------------------------------------图像 “http://localhost/GuestTest08/code.php “因其本身有错误无法显示

解决方案 »

  1.   

    引用这个验证码的页面可能进行写操作了, 问题应该出现在这里header("Content-Type:image/png"),这之前是不许有写操作的,而你的code.php里面没有echo这类的东西,我猜可能是引用code.php这个的页面有页面写操作吧。建议不直接输出,而是生成随机的图片文件,用img的src调用也可以
      

  2.   

    //header("Content-Type:image/png");注释掉类型声明,一切将暴露无遗
      

  3.   

    require dirname(__FILE__).'/includes/common.inc.php'; //此文件中引用了 global.func.php 文件我把你这句代码注释掉是可以产生验证码的,你的common.inc.php里有些什么东西???
      

  4.   

    先检查调用的各个文件是否有bom头,或已经有输出.也可以这样试试ob_start();//打开输出缓存,这句最好放开头
    require dirname(__FILE__).'/include/common.inc.php';//其他一些不确定是否有输出的语句放这里ob_end_clean();//清空缓存_code();