源程序如下:
@header("Content-Type:image/png");
session_start();
$_SESSION['authnum'] = '';
$str = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
$list = explode(",", $str);
for($i=0; $i<4; $i++){
$randnum = rand(0, 62);
$authnum .= $list[$randnum];
}
$_SESSION['authnum'] = strtolower($authnum);
$im = @imagecreate(40, 20) or die("Cant's initialize new GD image stream!");
$text_color = imagecolorallocate($im, 255, 255, 255); //文本顔色
$background_color01 = imagecolorallocate($im, 255, 0, 0); //背景色1
$background_color02 = imagecolorallocatealpha($im, 255, 255, 255, 127); //背景色2
$noise_color = imagecolorallocate($im, 200, 200, 200); //干扰顔色
imagefill($im, 0, 0, $background_color02); //区域填充
imagestring($im, 5, 2, 2, $authnum, $text_color);
/*for($i=0; $i<400; $i++){ //加入干扰象素
imagesetpixel($im, rand()%90 , rand()%30 , $noise_color);
}*/
imagepng($im);
imagedestroy($im);
注释掉@header("Content-Type:image/png");,单独运行程序,发现错误:
Notice: Undefined variable: authnum in D:\WAMP\wwwroot\mei-de\admin\authnum.php on line 10
报错说是未定义变量authnum,之前用的是PHP5.2.6同样的程序也未有报错,怪哉。
在程序前定义好authnum变量好,程序OK。