`
lyongde
  • 浏览: 90585 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

从PHP5.2.6升级PHP5.3.1,验证码程序出错了。

    博客分类:
  • PHP
阅读更多

源程序如下:

 

@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。

分享到:
评论
4 楼 fyland 2010-02-02  
在验证码里面有数字和字母的时候,最好把数字0和字母o、数字1和字母l去掉,不然有些字体显示出来的,简直很难分清。
3 楼 roastduck 2010-01-09  
$authnum .= $list[$randnum];

等价于
$authnum = $authnum.$list[$randnum];
等式后的$authnum确实有点凭空蹦出来的感觉.个人还是挺支持变量先定义/声明再使用的规定.
$authnum = '';
$authnum.= $list[$randnum];

这样阅读起也越比较清晰.
  顺便,偷偷乐下自己的代码除了4到5过渡时需要做修改外还没遇到过因为换版本要改代码的问题.


2 楼 lyongde 2010-01-06  
GOOD,3QS
1 楼 szcjlssx 2009-12-15  
PHP5.3变量声明变严格了,呵呵
楼主的代码忍不住回个:获取随机字符串
$chars=array_merge(range(0,9),range('A','Z'));//获取字符数组
shuffle($chars);//打乱数组
$randomChars=join("",array_slice($chars,0,4));//获取4个随机字符
//如果要随机从数组中抽取一个元素,只要用array_rand就行了

相关推荐

Global site tag (gtag.js) - Google Analytics