php 图片验证码实例
时间:2016-05-07
本文将通过实例讲解使用PHP生成图片验证码,并介绍生成验证码的函数,需要的码农可以参考下。
本文以实例演示5种验证码,并介绍生成验证码的函数。PHP生成验证码的原理:通过GD库,生成一张带验证码的图片,并将验证码保存在Session中。
1、HTML
5中验证码HTML代码如下:
<div class="demo">
<h3>1、数字验证码</h3>
<p>验证码:<input type="text" class="input" id="code_num" name="code_num" maxlength="4" /> <img src="code_num.php" id="getcode_num" title="看不清,点击换一张" align="absmiddle" /></p>
<p><input type="button" class="btn" id="chk_num" value="提交" /></p>
</div>
<div class="demo">
<h3>2、数字+字母验证码</h3>
<p>验证码:<input type="text" class="input" id="code_char" maxlength="4" /> <img src="code_char.php" id="getcode_char" title="看不清,点击换一张" align="absmiddle" /></p>
<p><input type="button" class="btn" id="chk_char" value="提交" /></p>
</div>
<div class="demo">
<h3>3、中文验证码</h3>
<p>验证码:<input type="text" class="input" id="code_zh" maxlength="4" /> <img src="code_zh.php" id="getcode_zh" title="看不清,点击换一张" align="absmiddle" /></p>
<p><input type="button" class="btn" id="chk_zh" value="提交" /></p>
</div>
<div class="demo">
<h3>4、仿google验证码</h3>
<p>验证码:<input type="text" class="input" id="code_gg" maxlength="4" /> <img src="code_gg.php" id="getcode_gg" title="看不清,点击换一张" align="absmiddle" /></p>
<p><input type="button" class="btn" id="chk_gg" value="提交" /></p>
</div>
<div class="demo">
<h3>5、算术验证码</h3>
<p>验证码:<input type="text" class="input" id="code_math" maxlength="4" /> <img src="code_math.php" id="getcode_math" title="看不清,点击换一张" align="absmiddle" /></p>
<p><input type="button" class="btn" id="chk_math" value="提交" /></p>
</div>
2、js验证
$(function() {
$("#getcode_num").click(function() { //数字验证
$(this).attr("src", 'code_num.php?' + Math.random());
});
$("#chk_num").click(function() {
var code_num = $("#code_num").val();
$.post("chk_code.php?act=num", {
code: code_num
},
function(msg) {
if (msg == 1) {
alert("验证码正确!");
} else {
alert("验证码错误!");
}
});
});
//数字+字母验证
$("#getcode_char").click(function() {
$(this).attr("src", 'code_char.php?' + Math.random());
});
$("#chk_char").click(function() {
var code_char = $("#code_char").val();
$.post("chk_code.php?act=char", {
code: code_char
},
function(msg) {
if (msg == 1) {
alert("验证码正确!");
} else {
alert("验证码错误!");
}
});
});
//中文验证码
$("#getcode_zh").click(function() {
$(this).attr("src", 'code_zh.php?' + Math.random());
});
$("#chk_zh").click(function() {
var code_zh = escape($("#code_zh").val());
$.post("chk_code.php?act=zh", {
code: code_zh
},
function(msg) {
if (msg == 1) {
alert("验证码正确!");
} else {
alert("验证码错误!");
}
});
});
//google验证
$("#getcode_gg").click(function() {
$(this).attr("src", 'code_gg.php?' + Math.random());
});
$("#chk_gg").click(function() {
var code_gg = $("#code_gg").val();
$.post("chk_code.php?act=gg", {
code: code_gg
},
function(msg) {
if (msg == 1) {
alert("验证码正确!");
} else {
alert("验证码错误!");
}
});
});
//算术验证
$("#getcode_math").click(function() {
$(this).attr("src", 'code_math.php?' + Math.random());
});
$("#chk_math").click(function() {
var code_math = $("#code_math").val();
$.post("chk_code.php?act=math", {
code: code_math
},
function(msg) {
if (msg == 1) {
alert("验证码正确!");
} else {
alert("验证码错误!");
}
});
});
});
3、PHP生成验证码
session_start();
getCode(4,60,20);
function getCode($num,$w,$h) {
$code = "";
for ($i = 0; $i < $num; $i++) {
$code .= rand(0, 9);
}
//4位验证码也可以用rand(1000,9999)直接生成
//将生成的验证码写入session,备验证时用
$_SESSION["helloweba_num"] = $code;
//创建图片,定义颜色值
header("Content-type: image/PNG");
$im = imagecreate($w, $h);
$black = imagecolorallocate($im, 0, 0, 0);
$gray = imagecolorallocate($im, 200, 200, 200);
$bgcolor = imagecolorallocate($im, 255, 255, 255);
//填充背景
imagefill($im, 0, 0, $gray);
//画边框
imagerectangle($im, 0, 0, $w-1, $h-1, $black);
//随机绘制两条虚线,起干扰作用
$style = array ($black,$black,$black,$black,$black,
$gray,$gray,$gray,$gray,$gray
);
imagesetstyle($im, $style);
$y1 = rand(0, $h);
$y2 = rand(0, $h);
$y3 = rand(0, $h);
$y4 = rand(0, $h);
imageline($im, 0, $y1, $w, $y3, IMG_COLOR_STYLED);
imageline($im, 0, $y2, $w, $y4, IMG_COLOR_STYLED);
//在画布上随机生成大量黑点,起干扰作用;
for ($i = 0; $i < 80; $i++) {
imagesetpixel($im, rand(0, $w), rand(0, $h), $black);
}
//将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成
$strx = rand(3, 8);
for ($i = 0; $i < $num; $i++) {
$strpos = rand(1, 6);
imagestring($im, 5, $strx, $strpos, substr($code, $i, 1), $black);
$strx += rand(8, 12);
}
imagepng($im);//输出图片
imagedestroy($im);//释放图片所占内存
}
- 如何在不影响asp.net默认安全性的前提下使用ckeditor/fckeditor?
- Linux下防御DDOS攻击的操作梳理
- Android新手之旅(8) ListView的使用
- 更换Ubuntu源为国内源的操作记录
- Android新手之旅(8) ListView的使用
- CKEditor/CKFinder升级心得
- Docker容器学习梳理-Dockerfile构建镜像
- 再谈web开中几种经典的大文件上传组件
- Nginx负载均衡中后端节点服务器健康检查的操作梳理
- Linux系统下CPU使用(load average)梳理
- 基于组件的.NET技术(5)
- Silverlight与WPF中BeginInvoke的差异
- Linux下部署SSH登录时的二次身份验证环境记录(利用Google Authenticator)
- Linux下DNS简单部署(主从域名服务器)
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC
- Pytorch转NCNN的流程记录
- 算法图解:如何找出栈中的最小值?
- 滴滴出行二面笔试题
- spring JdbcTemplate 查询,参数中使用BeanPropertyRowMapper的作用
- 百万级类别的分类模型的拆分训练
- 关于Spring定义的preDestroy修饰的方法不执行,有以下两种原因,总有一款适合你
- CSP201912-2-回收站选址题目解析-Java ,
- Spring boot框架快速入门
- SpringBoot 跨域问题:Access to XMLHttpRequest at ‘***‘ from origin ‘***‘ has been blocked by CORS policy
- 火车购票-CSP201609-2-Java
- 从后端开发人员的视角:最浅显的理解 Vue
- Sublime怎么默认显示文件路径
- Git 删除已提交的文件
- Sublime 怎么装SQL语法检测器
- Vue 设置环境变量和模式