<?php
PHPChina 开源社区门户&~L9Bhpfsession_start();
PHPChina 开源社区门户*D%IH
i r
Hsession_register("login_check_number");
2e$n.[s}Y)p0//昨晚看到了chianren上的验证码效果,就考虑了一下,用
PHP的GD库完成了类似功能
PHPChina 开源社区门户xP0J
Tp}4{//先成生背景,再把生成的验证码放上去
PHPChina 开源社区门户 Jrb\eq9j`h$img_height=70; //先定义图片的长、宽
1F.kWOo8k+K0$img_width=25;
PHPChina 开源社区门户?(el&qU5? //生产验证码字符
PHPChina 开源社区门户&tJO.c\
['A$ychar="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";
KD
zK `'Rg0$list=explode(",",$ychar);
PHPChina 开源社区门户`b
maS|wfor($i=0;$i<4;$i++){
PHPChina 开源社区门户 {2XB&m&Z%G#{;yo $randnum=rand(0,35);
g!K*?
R clWi O0 $authnum.=$list[$randnum];
]!fh5solz0}
PHPChina 开源社区门户L"i;efE4A @//把验证码字符保存到session
5N2YId+L7pZ+_Z0$_SESSION["login_check_number"] = $authnum;
PHPChina 开源社区门户&X-\g Wp&nPHPChina 开源社区门户K,JNi^0P*IPHPChina 开源社区门户H
E[~d-J~s.~$aimg = imagecreate($img_height,$img_width); //生成图片
5p&MZ6hi?
Ay0imagecolorallocate($aimg, 255,255,255); //图片底色,ImageColorAllocate第1次定义颜色PHP就认为是底色了
f'V&V
h*ln0$black = imagecolorallocate($aimg, 0,0,0); //定义需要的黑色
-z(y/O3v n:B2o&K5esu0.Q.M(XSH8{0PHPChina 开源社区门户"YX D+H4Ehufor ($i=1; $i<=100; $i++) {//画100个睡觉颜色的 *
PHPChina 开源社区门户(t~H0az![ imagestring($aimg,1,mt_rand(1,$img_height),mt_rand(1,$img_width),"@",imagecolorallocate($aimg,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));
-?
s4n;[UD#@I;d2Jz7C"~0}
jL;K r6`9n v0,jf
y S8k!W7mY3G0//为了区别于背景,这里的颜色不超过200,上面的不小于200
PHPChina 开源社区门户zc`1Yj_!a(\0R%@0Lfor ($i=0;$i<strlen($authnum);$i++){
4|.T$F:o5T[1o%]a0 imagestring($aimg, mt_rand(3,5),$i*$img_height/4+mt_rand(2,7),mt_rand(1,$img_width/2-2), $authnum[$i],imagecolorallocate($aimg,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)));
+\#BgP}I*cO.Q0}
PHPChina 开源社区门户0w t,No)xXjHFimagerectangle($aimg,0,0,$img_height-1,$img_width-1,$black);//画一个矩形
PHPChina 开源社区门户z8``-R9w%}vOHeader("Content-type: image/PNG");
Ej G^eoO f0ImagePNG($aimg); //生成png格式
Ir p6@y0ImageDestroy($aimg);
2a6fL:fkI0PHPChina 开源社区门户e^ H9V:y(\~M5\?>