2009年10月7日 星期三

PHP:隨機產生驗證碼登錄實例

?>session_start();

//不存在imageCreate函数则认为当前环境不支持GD库

if (function_exists('imagecreate')) {

//产生4个字符的随机字符串作为验证码

$str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

$code = array();

for ($i=0; $i<4;>

$code[] = $str[mt_rand(1,strlen($str))];

}

//将验证码写入到Session,忽略大小写

$_SESSION['vdcode'] = strtolower(implode('',$code));

$width = 50; //图片宽度

$height = 20; //图片高度

$im = ImageCreate($width,$height); //创建图形

ImageColorAllocate($im,255,255,255); //填充背景颜色为白色

//用淡色给图形添加杂色

for ($i=0; $i<100;>

$pxcolor = ImageColorAllocate($im,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));

ImageSetPixel($im,mt_rand(0,$width),mt_rand(0,$height),$pxcolor);

}

//用深色调绘制边框

$bordercolor = ImageColorAllocate($im,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));

ImageRectangle($im,0,0,$width-1,$height-1,$bordercolor);

//用比较明显的颜色写上验证码文字

$offset = 5;

foreach ($code as $char) {

$textcolor = ImageColorAllocate($im,mt_rand(0,250),mt_rand(0,150),mt_rand(0,250));

ImageChar($im,5,$offset,2,$char,$textcolor);

$offset += 10;

}

//禁止缓存

header("pragma:no-cachern");

header("Cache-Control:no-cachern");

header("Expires:0rn");

//检查系统支持的文件类型,优先级为PNG->JPEG->GIF

if (ImageTypes() & IMG_PNG) {

header('Content-Type:image/png');

ImagePNG($im);

} elseif (ImageTypes() & IMG_JPEG) {

header('Content-Type:image/jpeg');

ImageJPEG($im);

} else {

header('Content-Type:image/gif');

ImageGif($im);

}

} else {

//不支持GD库,则输出默认验证码ABCD

$_SESSION['vdcode'] = 'abcd';

header('Content-Type:image/jpeg');

$fp = fopen('vdcode.jpg','rb');

echo fread($fp,filesize('vdcode.jpg'));

fclose($fp);

}

?>

資料來源:請點這

沒有留言:

張貼留言