验证码实例如下看不懂的先看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验证码实例的更多相关文章

  1. JavaWeb学习之转发和重定向、会话技术:cookie、session、验证码实例、URLConnection使用(下载网页)(4)

    1.转发和重定向 HttpServletResponse response 转发: RequestDispatcher dispatcher = request.getRequestDispatche ...

  2. C#生成验证码实例

    常用生成验证码实例封装: /// <summary> /// 生成内存位图 /// </summary> /// <param name="Code" ...

  3. Thinkphp框架拓展包使用方式详细介绍--验证码实例(十一)

    原文:Thinkphp框架拓展包使用方式详细介绍--验证码实例(十一) 拓展压缩包的使用方式详细介绍 1:将拓展包解压:ThinkPHP3.1.2_Extend.zip   --> 将其下的 \ ...

  4. 5种PHP生成图片验证码实例

    5种PHP生成图片验证码实例,包括数字验证码.数字+字母验证码.中文验证码.仿google验证码和算术验证码,PHP生成验证码的原理:通过GD库,生成一张带验证码的图片,并将验证码保存在Session ...

  5. DAY5 python内置函数+验证码实例

    内置函数 用验证码作为实例 字符串和字节的转换 字符串到字节 字节到字符串

  6. php_curl模拟登录有验证码实例

    <?php/** * @author 追逐__something * @version $id */define('SCRIPT_ROOT',dirname(__FILE__).'/');$ac ...

  7. Django学习-25-图片验证码实例

    处理流程 用户请求网页 --> 后台发送登录界面的静态页面 --> 后台在内存中生成验证码 --> 验证码保存在用户对应的Session中 --> 返回验证码图片到前端 用户登 ...

  8. 使用C#的aforge类库识别验证码实例

    一: 验证码处理 1.  一般处理原则 这种验证码为什么说跟没有一样,第一点:字体规范工整,第二点:不旋转扭曲粘连,第三点:字体颜色单一,下面看处理步骤. 这里要注意的是,aforge只接受像素格式为 ...

  9. spring mvc 使用kaptcha配置生成验证码实例

    SpringMVC整合kaptcha(验证码功能) 一.依赖 <dependency> <groupId>com.github.penggle</groupId> ...

随机推荐

  1. LOJ P10171 牧场的安排 题解

    每日一题 day6 打卡 Analysis 状压dp dp[i][j]+=dp[i-1][k]; #include<iostream> #include<cstdio> #in ...

  2. How to troubleshoot the "Could not create 'CDO.Message'" error message

     https://support.microsoft.com/en-us/kb/910360 Method 1: Make sure that the Cdosys.dll file is cor ...

  3. Luogu5072 [Ynoi2015]盼君勿忘 【莫队】

    题目描述:对于一个长度为\(n\)的序列,\(m\)次询问\(l,r,p\),计算\([l,r]\)的所有子序列的不同数之和\(\mathrm{mod} \ p\). 数据范围:\(n,m,a_i\l ...

  4. (1)打鸡儿教你Vue.js

    当今世界不会Vue.js,前端必定路难走 一个JavaScript MVVM库 以数据驱动和组件化的思想构建的 Vue.js是数据驱动 HTML/CSS/JavaScript/ES6/HTTP协议/V ...

  5. 关闭linux下的主板响声(主板蜂鸣器)

    在从deepin的kdd桌面换到xfce桌面后,命令行和界面操作上动不动会让主机响一声. manjaro的xfce版也是如此,不知道是不是linux下xfce的通病. 主要是搜索的时候百度的结果很奇葩 ...

  6. jquery validate 表单验证插件 代码

    /* 表单验证 */ var signupFormValidator = $("#signupForm").validate({ /* 自定义验证规则 */ rules : { o ...

  7. Mybatis基础-完整CRUD操作

    步骤一:mybatis基本配置 1)创建Java项目,在lib下导入mybatis所需要的Jar包,包括链接mysql的mysql-connector-java-5.1.7-bin.jar. 2)在s ...

  8. FLUENT导入CHEMKIN机理的单位问题【转载】

    转载自:http://blog.sina.com.cn/s/blog_4a0a8b5d0101pj3c.html CHEMKIN机理导入后,发现速率常数全变了,那么他们是怎样变化的呢? FLUENT中 ...

  9. Tecplot如何提取三维图中某条线的数据【转载】

    转载自:http://blog.sina.com.cn/s/blog_9de422500102v9by.html 截取线所在的面Data.Extract .slice from Plane,显示如下窗 ...

  10. MyBatis 与 Hibernate

    MyBatis 是一个优秀的基于 Java 的持久层框架,它内部封装了 JDBC,使开发者只需关注 SQL 语句本身,而不用再花费精力去处理诸如注册驱动.创建 Connection.配置 Statem ...