php实现验证码

简单纯数字验证码

<?php
$image = imagecreate(50,25);
imagecolorallocate($image,0,0,0);
$color =  imagecolorallocate($image,255,255,255);
$code = mt_rand(1000,9999);
session_start();
$_SESSION['code'] = $code;
imagestring($image,4,5,5,$code,$color);
header("content-type:image/png");
imagepng($image);
?>


数字+字母验证码:

<?php
$str="QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
$image=imagecreate(50,25);
imagecolorallocate($image,mt_rand(0,125),mt_rand(0,125),mt_rand(0,125));
$color = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
for($i=1;$i<=4;$i++) {
    $date=$str[mt_rand(0,strlen($str)-1)];
    $code.=$date;
}
session_start();
$_SESSION['code'] = $code;
imagestring($image,4,8,4,$code,$color);
for($i=1;$i<=30;$i++) {
imagesetpixel($image,mt_rand(0,50),mt_rand(0,25),mt_rand(125,200));
}
for($i=1;$i<=mt_rand(1,5);$i++) {
imageline($image,mt_rand(0,50),mt_rand(0,25),mt_rand(0,50),mt_rand(0,25),mt_rand(100,150));
}
header("content-type:image/png");
imagepng($image);
?>

数字+字母验证码(各字母颜色不同):

<?php
$str="QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
$image=imagecreate(50,25);
imagecolorallocate($image,mt_rand(0,125),mt_rand(0,125),mt_rand(0,125));
$color[0] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
$color[1] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
$color[2] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
$color[3] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
for($i=0;$i<4;$i++) {
    $date=$str[mt_rand(0,strlen($str)-1)];
    $code.=$date;
    imagestring($image,5,6+$i*10,4,$code[$i],$color[$i]);
}
session_start();
$_SESSION['code'] = $code;
for($i=1;$i<=30;$i++) {
imagesetpixel($image,mt_rand(0,50),mt_rand(0,25),mt_rand(125,200));
}
for($i=1;$i<=mt_rand(1,5);$i++) {
imageline($image,mt_rand(0,50),mt_rand(0,25),mt_rand(0,50),mt_rand(0,25),mt_rand(100,150));
}
header("content-type:image/png");
imagepng($image);
?>

流程:

1.设置字符集

2.创建图片资源集

3.设置图片背景颜色

4.随机取出字符集中的字符

5.开启SESSION用SESSION变量记录取出的字符集

6.把取出的字符集加入图片,增加字体颜色

7.添加干扰点

8.添加干扰线

9.填写header文件头

10.输出图片

posted @ 2011-09-14 15:26  Zox  阅读(529)  评论(0编辑  收藏  举报