各位大侠,小弟刚刚接触PHP,碰到一个验证码的问题, 我直接输入imgcode.inc.php可以正常显示验证码,可是我在另外一个文件中INCLUDE 进去,就显示不了。
验证码文件 imgcode.inc.php<?
session_register("valicode");
$width=50;    //先定义图片的长、宽 
$height= isset($_REQUEST['height'])?$_REQUEST['height']:18;
$rand_str = "";
for($i=0;$i<4;$i++){
$rand_str .= chr(mt_rand(48,57));
}
if(function_exists("imagecreate")){ $_SESSION["valicode"]=strtolower($rand_str);//注册session

$img = imagecreate($width,$height);//生成图片
    imagecolorallocate($img, 255,255,255);  //图片底色,ImageColorAllocate第1次定义颜色PHP就认为是底色了 
    $black = imagecolorallocate($img,127,157,185);        

    for ($i=1; $i<=50; $i++) { //背景显示雪花的效果
        imagestring($img,1,mt_rand(1,$width),mt_rand(1,$height),"#",imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255))); 
    } 

for($i=0;$i<4;$i++){ //加入文字
imagestring($img, mt_rand(2,5), $i*10+6, mt_rand(2,5), $rand_str[$i],imagecolorallocate($img,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)));
 }

   imagerectangle($img,0,0,$width-1,$height-1,$black);//先成一黑色的矩形把图片包围

if(function_exists("imagejpeg")){
header("content-type:image/jpeg\r\n"); imagejpeg($img); 
}else{ 
header("content-type:image/png\r\n"); imagepng($img); 
}

imagedestroy($img);

}else{
$_SESSION["valicode"]="1234";
header("content-type:image/jpeg\r\n");
$fp = fopen("./valicode.bmp","r");
echo fread($fp,filesize("./validate.bmp"));
fclose($fp);
}?>plugins index.php<?
include ("../core/config.inc.php");
$q = !isset($_REQUEST['q'])?"":$_REQUEST['q'];
$file = "html/".$q.".inc.php";
if (file_exists($file)){
include_once ($file);         exit;
}?>调用方法:plugins/index.php?q=imgcode

解决方案 »

  1.   

    调用方式
    <img src='plugins/index.php?q=imgcode'>他输出的是图片数据流,不能与网页的文本流混在一起
      

  2.   

    文件地址是不是html/imgcode.inc.php,估计是路径出问题了还要一个可能就是session-register前面有输出,这个前面不能有输出的(不过楼主如果没有错误提示应该不是这个问题)
      

  3.   

    1.imagecreate函数存在吗? 如果不存在,则会读取valicode.bmp,这个路径是相对路径,所以plugin/index.php包含html/imagecode.php的时候就是以plugin.php为准了.路径就变了.2.plugin/index.php 包含了 ../core/config.inc.php ,请确保这两个文件没有其他输出.如果是utf-8编码,请注意检查文件是否有bom头.(注:记事本编辑过的utf-8的文本文件都会被添加bom头.)