php验证码实例
验证码实例如下看不懂的先看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;
?>
php验证码实例的更多相关文章
- JavaWeb学习之转发和重定向、会话技术:cookie、session、验证码实例、URLConnection使用(下载网页)(4)
1.转发和重定向 HttpServletResponse response 转发: RequestDispatcher dispatcher = request.getRequestDispatche ...
- C#生成验证码实例
常用生成验证码实例封装: /// <summary> /// 生成内存位图 /// </summary> /// <param name="Code" ...
- Thinkphp框架拓展包使用方式详细介绍--验证码实例(十一)
原文:Thinkphp框架拓展包使用方式详细介绍--验证码实例(十一) 拓展压缩包的使用方式详细介绍 1:将拓展包解压:ThinkPHP3.1.2_Extend.zip --> 将其下的 \ ...
- 5种PHP生成图片验证码实例
5种PHP生成图片验证码实例,包括数字验证码.数字+字母验证码.中文验证码.仿google验证码和算术验证码,PHP生成验证码的原理:通过GD库,生成一张带验证码的图片,并将验证码保存在Session ...
- DAY5 python内置函数+验证码实例
内置函数 用验证码作为实例 字符串和字节的转换 字符串到字节 字节到字符串
- php_curl模拟登录有验证码实例
<?php/** * @author 追逐__something * @version $id */define('SCRIPT_ROOT',dirname(__FILE__).'/');$ac ...
- Django学习-25-图片验证码实例
处理流程 用户请求网页 --> 后台发送登录界面的静态页面 --> 后台在内存中生成验证码 --> 验证码保存在用户对应的Session中 --> 返回验证码图片到前端 用户登 ...
- 使用C#的aforge类库识别验证码实例
一: 验证码处理 1. 一般处理原则 这种验证码为什么说跟没有一样,第一点:字体规范工整,第二点:不旋转扭曲粘连,第三点:字体颜色单一,下面看处理步骤. 这里要注意的是,aforge只接受像素格式为 ...
- spring mvc 使用kaptcha配置生成验证码实例
SpringMVC整合kaptcha(验证码功能) 一.依赖 <dependency> <groupId>com.github.penggle</groupId> ...
随机推荐
- Linux监控系统概览
自从Linux系统诞生之始,监控系统就随之出现. 当然说到监控系统,我们就必须聊到SNMP协议,SNMP分为管理端(NMP)和被管理端. 管理端周期性的到被监控端采集数据,被监控端还需要有权限收集数据 ...
- PHP 创建 MySQL 表
CREATE TABLE 语句用于创建 MySQL 表. 创建表前,我们需要使用 use myDB 来选择要操作的数据库: use myDB; 我们将创建一个名为 "MyGuests&quo ...
- js图片上传 的方法
先规划出框架 <div id="AQA" style="width:300px; height:200px; background-color:aquamarine ...
- Windows下有用的工具推荐
1.ConEmu多标签命令行工具https://download.fosshub.com/Protected/expiretime=1495446879;badurl=aHR0cDovL3d3dy5m ...
- DOM操作2
一.API和WebAPI API就是接口,就是通道,负责一个程序和其他软件的沟通,本质是预先定义的函数. Web API是网络应用程序接口.包含了广泛的功能,网络应用通过API接口,可以实现存储服务. ...
- bzoj 3629
给出数 $n$记 $f(x)$ 表示 $x$ 的因子和求出所有 $x$ 使得 $f(x) = n$考虑 $x = p_1 ^{a_1} * p_2 ^ {a_2} * \cdots * p_k ^ { ...
- 如何实现一个 windows 桌面动态壁纸
更新: 2018/08/31 WS_MOUSE_LL 钩子,实现底层壁纸交互效果. 一.介绍 国内玩家第一次看到动态壁纸,都是出于一款来自 Wallpaper Engine 的 Steam 程序. ...
- 数据结构实验之图论十:判断给定图是否存在合法拓扑序列(SDUT 2140)
分析:BFS判断是否有环. #include<bits/stdc++.h> using namespace std; typedef long long ll; int gra[200][ ...
- 在学习ROS过程中碰到的一些问题--1
好了,这是接触ROS的第三周了,初步了解了一下ROS,很多问题自己还是无法解决,但是想着很久没有在blog上记录自己的学习过程,就先胡乱写一下吧.^-^ 1.关于ROS各种基本概念的理解 这方面知识建 ...
- Mac 10.14.5系统偏好设置安全性与隐私不展示任何来源解决办法
Mac新系统升级(10.14.5)后未从appstore下载的软件在安装时会提示安装包已损坏之类的东东,这是因为没有打开“设置”—“安全与隐私”中的“任何来源”造成的,可是升级后的10.14.5却没有 ...