验证码实例如下看不懂的先看gd库的那一篇,session不懂的看from提交的数据都哪里了那一篇,欢迎关注

index.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form name="form" method="post" action="">
验证码(大写):<br>
<img src="hcon.php" width="70" height="18" border="0" align="bottom">
<input type="text" name="yanzhengma">
<br>
<input type="Submit" name="Submit" value="登录" >
</form>
</body>
</html>
<?php
session_start();
if(isset($_POST["Submit"]) && $_POST["Submit"]!=""){
$yanzhengma=$_POST["yanzhengma"]; if($yanzhengma=="")
{ echo "<script> alert('验证码为空');window.location.href='index.php';</script>";}
else if($yanzhengma!=$_SESSION["jiancha"]){
echo "<script> alert('输错了');window.location.href='index.php';</script>";
}
else{
echo "<script> alert('登录成功');window.location.href='index.php';</script>";
} } ?>

yanzhengma.php

<?php
class yanzhengma2{
protected $width;
protected $height;
protected $im;
protected $len;
protected $code; public function __construct($width=100,$height=30,$len=5){
$this->width=$width;
$this->height=$height;
$this->len=$len;
} public function render(){
$im= imagecreatetruecolor($this->width,$this->height);
$color=imagecolorallocate($im, 200,200,200);
imageFill($this->im=$im,0,0,$color);//画布背景填充颜色,默认黑色
$this->text();
$this->line();
$this->pix();
$this->show();
return $this->code; } //显示渲染
protected function show(){
header('Content-type:image/png');
imagepng($this->im);
} //绘制验证码
protected function text(){
$font=$font="c:/windows/fonts/simhei.ttf";
$text='abcdefghigklmnopqrstuvwxyz0123456789';
for($i=0;$i<$this->len;$i++){
$x=$this->width/$this->len;
$angle=mt_rand(-20,20);
$this->code.=$textn=strtoupper($text[mt_rand(0,strlen($text)-1)]);
$box=imagettfbbox(20,$angle,$font,$textn); imagettftext(
$this->im,
20,$angle,
$x*$i+10,
$this->height/2-($box[7]-$box[1])/2,
$this->textcolor(),
$font,
$textn
);
}
}
//绘制干扰点
protected function pix(){
for($i=0;$i<300;$i++){
imagesetpixel($this->im,mt_rand(0,$this->width),mt_rand(0,$this->height),$this->color());
} }
//绘制干扰线
protected function line(){ for($i=0;$i<6;$i++){
imagesetthickness($this->im,mt_rand(1,2));
imageline($this->im,
mt_rand(0,$this->width),
mt_rand(0,$this->height),
mt_rand(0,$this->width),
mt_rand(0,$this->height),
$this->color()
);
}
}
//生成随机颜色
protected function color(){
return imagecolorallocate($this->im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
} //生成随机文字颜色(深)
protected function textcolor(){
return imagecolorallocate($this->im,mt_rand(0,50),mt_rand(0,50),mt_rand(0,50));
}
} ?>

hcon.php

<?php
session_start();
include 'yanzhengma.php';
$yanzhengma=new yanzhengma2(200,40);
$code=$yanzhengma->render();
$_SESSION["jiancha"]=$code;
?>

最新文章

  1. Android数据加密之Base64编码算法
  2. linux学习中遇到的各种故障与解决方法
  3. Windows Phone &amp; Windows App应用程序崩溃crash信息抓取方法
  4. Java Web开发 之JavaBean整理
  5. Http(1)
  6. 正则匹配 sql语句参数
  7. JSP标准标签库(JSTL)--国际化标签库 fmt
  8. 赋值号和printf
  9. HDU 1010生成树
  10. 进程 day36
  11. 接口、抽象类、泛型、hashMap
  12. 任务调度之Timer与TimerTask配合
  13. map filter 的func 放在前面
  14. 【大数据应用技术】作业八|爬虫综合大作业Molly134
  15. Python初学者第十五天 文件处理3
  16. git用户限制ssh登录服务器
  17. logging记录日志
  18. selector模块
  19. spring 源码下载
  20. importlib 模块导入

热门文章

  1. 约瑟夫问题O(n)/O(mlogn)
  2. [Angular] Show a Loading Indicator for Lazy Routes in Angular
  3. Linux 硬件软件时间同步
  4. Linux Swap是干嘛的?
  5. (6)打鸡儿教你Vue.js
  6. RESTFUL API 安全认证方式
  7. mui Picker DtPicker 固定内容以及变动内容Picker 不同情况下的应用 提高效率
  8. ICEM-带肋圆柱
  9. SSM框架的配置Spring+Springmvc +Mybatis
  10. cgdb UTF-8乱码