seccode.php:
<?php
session_start();
$act = $_GET['act'];
if($act == "init")
{
Header("Content-type: image/png");
srand(microtime() * 100000);
$login_check_number = strval(rand("1111","9999"));
$_SESSION["login_check_number"]=$login_check_number;
$h_img = imagecreate(40,17);
$c_black = ImageColorAllocate($h_img, 0,0,0);
$c_white = ImageColorAllocate($h_img, 255,255,255);
imageline($h_img, 1, 1, 350, 25, $c_black);
imagearc($h_img, 200, 15, 20, 20, 35, 190, $c_white);
imagestring($h_img, 5, 2, 1, $login_check_number, $c_white);
ImagePng($h_img);
ImageDestroy($h_img); die();
}
使用login.php
验证码:<img src=seccode.php?act=init&num=1717 ><input type="text" name="safecode" maxlength="4" />
<?php
session_start();
$act = $_GET['act'];
if($act == "init")
{
Header("Content-type: image/png");
srand(microtime() * 100000);
$login_check_number = strval(rand("1111","9999"));
$_SESSION["login_check_number"]=$login_check_number;
$h_img = imagecreate(40,17);
$c_black = ImageColorAllocate($h_img, 0,0,0);
$c_white = ImageColorAllocate($h_img, 255,255,255);
imageline($h_img, 1, 1, 350, 25, $c_black);
imagearc($h_img, 200, 15, 20, 20, 35, 190, $c_white);
imagestring($h_img, 5, 2, 1, $login_check_number, $c_white);
ImagePng($h_img);
ImageDestroy($h_img); die();
}
使用login.php
验证码:<img src=seccode.php?act=init&num=1717 ><input type="text" name="safecode" maxlength="4" />
php.ini文件里面,;extension=php_gd2.dll 把前面的分号去丢。重启服务器
放同一目录文件下就可以用了
//创建一个新图形
$cn = imagecreate(200,100);
//设置背景,分配颜色
$bgColor = imagecolorallocate($cn, 255, 255, 255);
//指定要使用的字体文件
$font = "simhei.ttf";
//用图形中输出字符串
$xl=20;
$xr=40;
$chars = array();
for($i=0;$i<4;$i++){
//设置随机种子
mt_srand((double)microtime()*1000000);
//取出中文字符
$c=mt_rand(176,215);
if($c==215){
$random=chr($c).chr(mt_rand(161,249));
}else{
$random=chr($c).chr(mt_rand(161,254));
}
//转换字符格式
$char = iconv("gb2312","utf-8",$random);
$chars[$i]=$char;
//随机生成颜色
$color = imagecolorallocate($cn, rand(0,255), rand(0,255), rand(0,255));
//把生成的中文字符写入到图形中
imagettftext($cn,20,rand(-60,60),rand($xl,$xr),50,$color,$font,$char);
$xl+=30;
$xr+=30;
}
//添加像素点干扰
for($i=0;$i<100;$i++){
$color = imagecolorallocate($cn, rand(0,255), rand(0,255), rand(0,255));
imagesetpixel($cn, rand(0,200) , rand(0,120) , $color);
}
//添加线干扰
for($i=0;$i<10;$i++){
$color = imagecolorallocate($cn, rand(0,255), rand(0,255), rand(0,255));
imageline($cn,rand(0,200),rand(0,200),rand(0,120),rand(0,120),$color);
}
//启动SESSION
session_start();
//使用session_destroy()结束所有SESSION
//保证此前创建的SESSION数据被删除
session_destroy();
//重新启动SESSION
session_start();
//把脚本生成的认证码,保存到SESSION中
session_register("chars");
//输出图形
header('Content-type: image/png');
imagepng($cn);
//释放资源
imagedestroy($cn);
?>
<table><tr
<TD width="20%" height="20" align="center" valign="middle" background="images/bj.gif" ><strong><font color="ffffff" size="2"><?php echo $authnum;?></font></strong></TD>
</tr></table>
提交的时候,比较一下是不是相等 如:
<?php if(strcmp($HTTP_POST_VARS['authnum'],$HTTP_POST_VARS['authinput'])==0){.....
} ?>
希望对你有帮助吧<?phpfunction createfile($filename,$contents){
$handle=fopen($filename,"w");
if(!fwrite($handle,$contents)){
echo"写入文件".$filename."失败!";
}
}srand((double)microtime()*1000000);
$filename="checkcode.txt";
$char="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";for($i=0;$i<7;$i++){
$number=mt_rand(1,72);
$contents.=$char[$number];
}
@createfile($filename,$contents);
$result=@file_get_contents($filename);
$image=imagecreate(160,50);
$black=imagecolorallocate($image,0,0,0);
$white=imagecolorallocate($image,255,255,255);
$rand_angle=mt_rand(0,5);
$mixcolor1=mt_rand(0,255);
$mixcolor2=mt_rand(0,255);
$mixcolor3=mt_rand(0,255);
$color = imagecolorallocate($image, rand(0,255), rand(0,255), rand(0,255));
$mixedcolor=imagecolorallocate($image,$mixcolor1,$mixcolor2,$mixcolor3);
@imagefilledrectangle($image,0,0,160,50,$white);
imagettftext($image,22,$rand_angle,6,40,$mixedcolor,"c:/WINDOWS/fonts/SCHLBKBI.TTF",$result);
//加入干扰像素
for($i=1;$i<800;$i++){
imagesetpixel($image,rand()%160,rand()%50,$color);
}
//加入干扰线
for($i=0;$i<10;$i++){
imageline($image,rand(0,160),rand(0,50),rand(0,160),rand(0,50),$color);
}imagepng($image);
?>
$handle=fopen($filename,"w");
if(!fwrite($handle,$contents)){
echo"写入文件".$filename."失败!";
}
}srand((double)microtime()*1000000);
$filename="checkcode.txt";
$char="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";for($i=0;$i<7;$i++){
$number=mt_rand(1,72);
$contents.=$char[$number];
}
@createfile($filename,$contents);
$result=@file_get_contents($filename);
$image=imagecreate(160,50);
$black=imagecolorallocate($image,0,0,0);
$white=imagecolorallocate($image,255,255,255);
$rand_angle=mt_rand(0,5);
$mixcolor1=mt_rand(0,255);
$mixcolor2=mt_rand(0,255);
$mixcolor3=mt_rand(0,255);
$color = imagecolorallocate($image, rand(0,255), rand(0,255), rand(0,255));
$mixedcolor=imagecolorallocate($image,$mixcolor1,$mixcolor2,$mixcolor3);
@imagefilledrectangle($image,0,0,160,50,$white);
imagettftext($image,22,$rand_angle,6,40,$mixedcolor,"c:/WINDOWS/fonts/SCHLBKBI.TTF",$result);
//加入干扰像素
for($i=1;$i<800;$i++){
imagesetpixel($image,rand()%160,rand()%50,$color);
}
//加入干扰线
for($i=0;$i<10;$i++){
imageline($image,rand(0,160),rand(0,50),rand(0,160),rand(0,50),$color);
}imagepng($image);
?>