php验证码实例
2024-09-01 14:32:51
验证码实例如下看不懂的先看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;
?>
最新文章
- Android数据加密之Base64编码算法
- linux学习中遇到的各种故障与解决方法
- Windows Phone &; Windows App应用程序崩溃crash信息抓取方法
- Java Web开发 之JavaBean整理
- Http(1)
- 正则匹配 sql语句参数
- JSP标准标签库(JSTL)--国际化标签库 fmt
- 赋值号和printf
- HDU 1010生成树
- 进程 day36
- 接口、抽象类、泛型、hashMap
- 任务调度之Timer与TimerTask配合
- map filter 的func 放在前面
- 【大数据应用技术】作业八|爬虫综合大作业Molly134
- Python初学者第十五天 文件处理3
- git用户限制ssh登录服务器
- logging记录日志
- selector模块
- spring 源码下载
- importlib 模块导入
热门文章
- 约瑟夫问题O(n)/O(mlogn)
- [Angular] Show a Loading Indicator for Lazy Routes in Angular
- Linux 硬件软件时间同步
- Linux Swap是干嘛的?
- (6)打鸡儿教你Vue.js
- RESTFUL API 安全认证方式
- mui Picker DtPicker 固定内容以及变动内容Picker 不同情况下的应用 提高效率
- ICEM-带肋圆柱
- SSM框架的配置Spring+Springmvc +Mybatis
- cgdb UTF-8乱码