在工作之余呢,想着为大家分享一些小小的实例,为人为己,共同学习,一起进步!好了,太多的煽情的话也不会说,呵呵,直接开始吧!
以下是原生php代码实现:
verity.php
<?php
header("content-type:text/html;charset=utf-8");
//开启session
session_start();
//准备画布
$im=imagecreatetruecolor(50,25);
//准备涂料
$black=imagecolorallocate($im,0,0,0);
$gray=imagecolorallocate($im,200,200,200);
//背景填充
imagefill($im,0,0,$gray);
//文字居中
$x=(50-10*4)/2;
$y=(25-5)/2+5;
//准备文字
$arr=array_merge(range(0,9),range('a','z'),range('A','Z'));
shuffle($arr);
$str=implode(array_slice($arr,0,4));
//把$str放入session中,方便所有页面中调用
$_SESSION['vstr']=$str;
$file="fonts/simsun.ttc";
imagettftext($im,10,0,$x,$y,$black,$file,$str);
//输出到浏览器上或保存起来
header("content-type:image/png");
imagepng($im);
//关闭画布
imagedestory($im);
?>
form.html
<html>
<head>
<meta charset="utf-8">
<title>登录</title>
<style>
table{
border-collapse:collapse;
}
img{
margin-top:5px;
}
</style>
</head>
<body>
<h2>用户登录页面</h2>
<hr>
<table width="500px" border="1px">
<form action="submit.php" method="post">
<tr>
<td>姓 名:</td>
<td><input type="text" name="username" id=""></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="password" id=""></td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text" name="vcode">
<img src="verity.php"></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
<td><input type="reset" value="重置"></td>
</tr>
</form>
</table>
</body>
</html>
submit.php
<?php
header("content-type:text/html;charset=utf-8");
session_start();
$code=strtolower($_POST['vcode']);
$str=strtolower($_SESSION['vstr']);
if($code==$str){
//页面跳转
echo "<script>location='http://www.baidu.com'</script>";
}else{
echo "<script>alert('验证码输入错误');</script>";
echo "<a href='form.html'>返回登录页面</a>";
}
?>