链接自:PHP讨客网
验证页面截图如下:
html页
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<form enctype="multipart/form-data" action="result.php" method="post" >
<p>
<input type="text" name="yzm" id="yzm" />
<img src="yzm.php" title="点击更换验证码" οnclick="this.src= 'yzm.php?id='+Math.random();" />
</p>
</form>
</body>
</html>
result.php 结果页
<?php
session_start();
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
if(strtoupper($_POST['yzm'])==$_SESSION['yzm']){
echo $_POST['yzm'];
echo $_SESSION['yzm'].'<br/>';
echo '结果正确';
}else{
echo '结果错误';
}
?>
yzm.php 验证页
<?php
session_start();
$width=100;
$height=30;
$img=imagecreate($width,$height);
$bg=imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //生成背景颜色
$color=imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//生成字体颜色imagecolorallocate() 用来生成颜色 第一次生成背景颜色 参数 (图像,红,绿,蓝)
$a=mt_rand(0,9);
$b=mt_rand(0,9);//随机在0-9之间 生成一个数字
$result=$a+$b;
$text=$a.'+'.$b.'=';
imagettftext($img,20,0,10,20,$color,'font/simkai.ttf',$text);//参数(图片,字体大小,倾斜角度,X轴,Y轴,字体颜色,字体来源,文字内容) imagettftext()函数
imagejpeg($img);
$_SESSION['yzm']=$result;
header("content-type:image/png");
?>