yzm.php文件代码:
<?php
$i = 0;
//检验验证码是否存在,不存在返回请输入验证码
if(isset($_POST['gcheck'])){
//检验验证码输入是否正确
if(!strtolower($_POST['gcheck'])==strtolower($_SESSION['scode'])){
$i = 1;
}
}else{
$i = 2;
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>验证码</title>
<script>
//检验验证码不能为空
function check(){
var gt = document.getElementById('gcheck');
if(gt.value == ''){
alert('验证码不能为空...');
gt.focus();
return false;
}
return true;
}
//验证码的输入框,获得焦点
window.onload = function(){
var gt = document.getElementById('gcheck');
gt.focus();
}
</script>
</head>
<body>
<form onsubmit="return check();" method="post" action="#" >
<table width="755" border="0" cellspacing="0" cellpadding="0">
<tr>
<th align="right">验证码:</th>
<td align="left"><input id="gcheck" class="Inputbox2" name="gcheck" />
<img src="inc/check.php" onClick="this.src='inc/check.php?'+new Date()">
<?php
echo $i==1 ? '验证码填写错误' : '';
echo $i==2 ? '验证码必须填写' : '';
?>
</td>
</tr>
<tr>
<td colspan="3">
<input type="submit" value="留言" style="margin-left:50px; width:50px; height:24px;border-radius:3px;">
<input type="reset" value="重置" style="margin-left:50px; width:50px; height:24px;border-radius:3px;">
</td>
</tr>
</table>
</form>
</body>
</html>
check.php文件代码
<?php
header('content-type:image/png');
session_start(); //开启session回话
$size = 4;
$w = 30 * $size; //定义宽度
$h = 35; //定义高度
$i = imagecreatetruecolor($w,$h); //imagecreatetruecolor — 新建一个真彩色图像
$bgc = imagecolorallocate($i,251,251,251); //imagecolorallocate — 为一幅图像分配颜色
imagefill($i,0,0,$bgc); //imagefill — 区域填充
//bool imagefill ( resource $image , int $x , int $y , int $color )
//imagefill() 在 image图像的坐标 x,y(图像左上角为 0, 0)处用 color颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。
//////定义随机函数-默认产生四个随机字母或者数字开始//////
function getStr($len = 4){
$str = 'abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789';
$s = '';
for($i=0;$i<$len;$i++){
$s.=$str{mt_rand(0,strlen($str)-1)};
}
return $s;
}
//////定义随机函数-默认产生四个随机字母或者数字开始//////
////////产生随机字母或数字-开始////////
$s = getStr($size);
$_SESSION['scode'] = $s;
for($n=0;$n<strlen($s);$n++){
//imagecolorallocate — 为一幅图像分配随机颜色
$c = imagecolorallocate($i,mt_rand(0,155),mt_rand(0,155),mt_rand(0,155));
//imagettftext — 用 TrueType 字体向图像写入文本
//array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
imagettftext($i,mt_rand(20,30),mt_rand(-15,30),25+($n*18),mt_rand(20,30),$c,'h.ttf',$s{$n});
}
////////产生随机字母或数字-结束////////
//////生成20个背景字母或数字,产生干扰效果-开始//////
$s = getStr(20);
for($n=0;$n<strlen($s);$n++){
//imagecolorallocatealpha — 为一幅图像分配颜色 + alpha
$c = imagecolorallocatealpha($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255),80);
//imagettftext — 用 TrueType 字体向图像写入文本
//array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
imagettftext($i,mt_rand(10,15),mt_rand(-15,30),mt_rand(0,$w),mt_rand(0,$h),$c,'h.ttf',$s{$n});
}
//////生成20个背景字母或数字,产生干扰效果结束//////
//////画随机椭圆-产生干扰效果-开始//////
//imagecolorallocatealpha — 为一幅图像分配随机颜色 + alpha
$c = imagecolorallocatealpha($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255),100);
$hh = mt_rand(10,20);
$ss = mt_rand(2,6);
for($n=0;$n<=$w;$n++){
$x = $n*2;
$y = 20 + $hh * sin($n*M_PI/20);
//imagefilledellipse — 画一椭圆并填充
imagefilledellipse($i,$x,$y,$ss,$ss,$c);
}
//////画随机椭圆-产生干扰效果-结束//////
for($n=0;$n<3;$n++){
//imagecolorallocatealpha — 为一幅图像分配随机颜色 + alpha
$c = imagecolorallocatealpha($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255),110);
$ss = mt_rand(40,100);
//imagefilledellipse — 画一椭圆并填充
imagefilledellipse($i,mt_rand(0,$w),mt_rand(0,$h),$ss,$ss,$c);
//imagecolorallocatealpha — 为一幅图像分配随机颜色 + alpha
$c = imagecolorallocatealpha($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255),100);
//imagefilledrectangle — 画一矩形并填充
imagefilledrectangle($i,mt_rand(0,$w),mt_rand(0,$h),mt_rand(0,$w),mt_rand(0,$h),$c);
}
imagepng($i); //imagepng — 以 PNG 格式将图像输出到浏览器或文件
imagedestroy($i); //imagedestroy — 销毁一图像