五一放假买票前登陆www.12306.cn上购票时 发现 人家做的验证码是一个数学表达式 ,很是好奇,于是乎自己用php图形技术(GD库)做了一个与其十分类似的验证码,与大家分享分享,具体代码如下:
,
code1.php 代码如下,该文件用来生成一张图片:
<?php
//启动session机制
session_start();
/**
* @author lixiuran 该函数用于返回大写汉字对应的数字
*
* Enter description here ...
* @param unknown_type $str
*/
function convertStrTonum($str){
switch ($str){
case "零";
case "0":
return 0;
break;
case "一";
case "壹";
case "1":
return 1;
break;
case "二";
case "贰":
case "2";
return 2;
break;
case "三";
case "叁":
case "3";
return 3;
break;
case "四";
case "肆":
return 4;
break;
case "五";
case "伍":
case "5";
return 5;
break;
case "六";
case "陆":
case "6";
return 6;
break;
case "七";
case "柒":
case "7":
return 7;
break;
case "八";
case "捌";
case "8":
return 8;
break;
case "九";
case "玖";
case "9":
return 9;
break;
}
}
//已png图像方式输出
header("content-type: image/png");
//创建空白图片
$im=imagecreate(110,26);
//定义一些颜色
$gray=imagecolorallocate($im,200,200,200);
$black=imagecolorallocate($im,255,255,255);
$randcolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
//定义一系列数组
$daxie1=array("零","一","二","三","四","五","六","七","八","九");
$daxie2=array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
$num=array("0","1","2","3","4","5","6","7","8","9");
$opers=array("加","+");
//随机产生计算结果
$result=rand(10,20);
//第一种表达式 例如: 一 加 ?=15
$thedaxie1=$daxie1[rand(0,count($daxie1)-1)];
$express1=$thedaxie1." ".$opers[rand(0,count($opers)-1)]." ? = $result";
$express1=iconv("gb2312","utf-8",$express1);
//第一种表达式 例如: 壹 加 ?=15
$thedaxie2=$daxie2[rand(0,count($daxie2)-1)];
$express2=$thedaxie2." ".$opers[rand(0,count($opers)-1)]." ? = $result";
$express2=iconv("gb2312","utf-8",$express2);
//第一种表达式 例如: 1 加 ?=15
$thenum=$num[rand(0,count($num)-1)];
$express3=$thenum." ".$opers[rand(0,count($opers)-1)]." ? = $result";
$express3=iconv("gb2312","utf-8",$express3) ;
//将随机产生的汉字转化成数字
$thedaxie1=convertStrTonum($thedaxie1);
$thedaxie2=convertStrTonum($thedaxie2);
$thenum=intval($thenum);
//计算三种问号的值
$wenhao=$result-$thenum;
$wenhao1=$result-$thedaxie1;
$wenhao2=$result-$thedaxie2;
//把三种问号放入session种,用于后面的验证
$_SESSION["wenhao"]=$wenhao;
$_SESSION["wenhao1"]=$wenhao1;
$_SESSION["wenhao2"]=$wenhao2;
//将这三个表达式放入数组中,用于随机产生
$express=array($express1,$express2,$express3);
//随即产生一种字体,前提在当前目录下新建一个fonts的文件夹,然后到windows -》fonts-》中复制几种字体即可
$fonts=array("fonts/msyh.ttf","fonts/simhei.ttf","fonts/stcaiyun.ttf","fonts/stxingka.ttf");
$randFont=$fonts[rand(0,count($fonts)-1)];
//网图片上输出拼接好的表达式,这句话最最重要
imagettftext($im,12,rand(-5,5),10,20,$black,$randFont,$express[rand(0,count($express)-1)]);
//给画布随机填充颜色
imagefill($im,0,0,$randcolor);
//以png方式输出图片
imagepng($im);
//销毁资源
imagedestroy($im);
?>
login.php 的代码如下:
<html>
<head>
<script type="text/javascript">
function checkForm(){
var checkcode=document.getElementById("checkcode").value;
//alert(checkcode);
if(checkcode==""){
alert("验证码不能为空");
return false;
}else if(isNaN(checkcode)){
alert("验证码必须为数字");
return false;
}
}
</script>
<style type="text/css">
a{text-decoration: none;font-size:12px;}
a:HOVER {
color:red;
font-size:12px;
font-weight:bold;
text-decoration: underline;
}
</style>
</head>
<body >
<h2>验证码测试案例</h2>
<Hr>
<form action="action1.php" method="post" οnsubmit="return checkForm()" >
请输入验证码:<input type="text" name="checkcode" size=4 id="checkcode"/>
<img src="code1.php" id="code" οnclick="this.src='code1.php?aa='+Math.random()"/>
<a href="#" οnclick="document.getElementById('code').src='code1.php?aa='+Math.random()"> 看不清,换一张</a>
<br><br>
<input type="submit" value="测试验证码" />
</form>
</body>
<hr>
</html>
action1.php 代码如下:
<?php
session_start();
if(isset($_POST["checkcode"])){
$numYouInput=$_POST["checkcode"];
}
if($numYouInput==$_SESSION["wenhao"]||$numYouInput==$_SESSION["wenhao1"]||$numYouInput==$_SESSION["wenhao2"]){
echo "验证码正确<hr>";
echo "<br><a href='login.php'>返回</a>";
}else{
echo "验证码错误<hr>";
echo "<br><a href='login.php'>返回</a>";
}
?>
如何使用呢?
把上面三个 code1.php action1.php login.php 三个文件 放入你的IIS服务器或apache服务器的www目录下,然后在你与三个文件同目录下
新建一个 fonts 文件夹,然后进入操作系统所在的磁盘,进入windows -》fonts -> 例如 把 黑体,华文彩云,华文细黑,微软雅黑,等字体放入刚建好的fonts文件夹,然后把字体的文件名放入数组中
$fonts=array("fonts/msyh.ttf","fonts/simhei.ttf","fonts/stcaiyun.ttf","fonts/stxingka.ttf");
在你的服务器上运行 login.php 即可
总结一点:世上无难事,只怕有心人..[IMG]http://b306.photo.store.qq.com/psb?/V146KxFl1uFHw6/bXlia2qbNsWd010PTAGWJuOzjOLYB7KM56rmwm3cAig!/b/YWp8aLZCNQAAYikcbbZ1NAAA[/IMG]