从PHP5.2.6升级PHP5.3.1,验证码程序出错了。
源程序如下:
@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。
在验证码里面有数字和字母的时候,最好把数字0和字母o、数字1和字母l去掉,不然有些字体显示出来的,简直很难分清。
等价于
等式后的$authnum确实有点凭空蹦出来的感觉.个人还是挺支持变量先定义/声明再使用的规定.
这样阅读起也越比较清晰. :wink: 顺便,偷偷乐下自己的代码除了4到5过渡时需要做修改外还没遇到过因为换版本要改代码的问题.
GOOD,3QS
PHP5.3变量声明变严格了,呵呵 楼主的代码忍不住回个:获取随机字符串