allen
专注于java ee技术,包括struts,jsf,webwork,spring,hibernate,ibatis
posts - 7,  comments - 9,  trackbacks - 0
因为防止恶意评论,所以要求加个验证码,要求简单,一般的20以内的加法即可,根据其它网上的验证码,修改了下:
<?php 
//session_start();
//生成验证码图片 
Header("Content-type: image/PNG"); 
srand((double)microtime()*1000000);//播下一个生成随机数字的种子,以方便下面随机数生成的使用
//session_start();//将随机数存入session中
$_SESSION['authnum']="";
$im = imagecreate(70,20) or die("Cant's initialize new GD image stream!"); //制定图片背景大小
$black = ImageColorAllocate($im, 0,0,0); //设定三种颜色
$white = ImageColorAllocate($im, 255,255,255); 
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,0,0,$gray); //采用区域填充法,设定(0,0)
//生成数字和字母混合的验证码方法
//$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";
$ychar="0,1,2,3,4,5,6,7,8,9";
$list=explode(",",$ychar);
$answer='';
$authnum=0;
for($i=0;$i<2;$i++){
$randnum=rand(0,10);
$authnum=$authnum+intval($randnum);
error_log("auth:$authnum :dddd:$randnum",0);
$answer=$answer.intval($randnum)."+";
}
$answer=rtrim($answer,"+");
$answer=$answer.'=?';
//$authnum=''.$answer;
//while(($authnum=rand()%100000)<10000); //生成随机的五们数
//将五位整数验证码绘入图片 
//$_COOKIE['authnum']=$authnum;
//error_log("auth:$authnum",0);
setCookie("authnum",$authnum,null,'/');
//imagestring($im, 5, 10, 3, $authnum, $black);
imagestring($im, 5, 10, 3, $answer, $black);
// 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 座标处(图像的左上角为 0, 0)。
//如果 font 是 1,2,3,4 或 5,则使用内置字体
/*
for($i=0;$i<200;$i++) //加入干扰象素 
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); 
 
 */
ImagePNG($im); 
ImageDestroy($im);

posted on 2011-12-12 10:17 robbin163 阅读(1124) 评论(0)  编辑  收藏 所属分类: WEB MVC

只有注册用户登录后才能发表评论。


网站导航:
 

<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(3)

随笔分类

随笔档案

文章分类

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜