php生成图片验证码的实例讲解
作者:lijiao 发布时间:2023-09-11 21:36:29
标签: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);//释放图片所占内存
}


猜你喜欢
- 1、类的定义创建一个rectangle.py文件,并在该文件中定义一个Rectangle类。在该类中,__init__表示构造方法。其中,s
- 结合邮件告警和页面展示,再多的域名证书到期情况即可立马知道代码示例:# coding: utf-8 # 查询域名证书到期情况import r
- 1. 稀疏矩阵的建立:coo_matrix()from scipy.sparse import coo_matrix# 建立稀疏矩阵data
- 但是怎么找到是哪个SQL语句的执行时间过长呢?可以通过MySQL Slow Log来找,详解如下。 首先找到MySQL的配置文件my.cnf
- 从事DBA的行业也有两年多了,在数据备份上无论是理论和实践上,都积
- 题目文件scores.csv包含十位学生的成绩单,表头是"姓名 语文 数学 英语"。请编程完成下述功能。1)计算每位学生
- 前言最近由于换工作,开始交接工作。整理以前的工作内容,由于组内就我一个在做go和大数据。 所以开发没有规划,当时是怎么快怎么来。go也是使用
- 相信大家对python-docx这个常用的操作docx文档的库都不陌生,它支持以内联形状(Inline Shape)的形式插入图片,即图片和
- 本文介绍使用aspjpeg组件实现图片的半透明描边的效果,描边效果演示:参数说明'big 原图路径(相对)'small 生成
- 排序查询(order by)电商中:我们想查看今天所有成交的订单,按照交易额从高到低排序,此时我们可以使用数据库中的排序功能来完成。排序语法
- 本文实例讲述了Python进阶之使用selenium爬取淘宝商品信息功能。分享给大家供大家参考,具体如下:# encoding=utf-8_
- 本文实例讲述了python os模块简单应用。分享给大家供大家参考,具体如下:举例中的目录形式如下所示:In [36]: pwdOut[36
- mysql对列求和mysql中,可以使用SELECT语句配合SUM()函数来对列求和,能够返回指定列值的总和,求和语法为&ldquo
- Js 的异步确实完美地解决了单线程的问题,但是同时也会带来许多问题。而且随着用的框架越来越多,越来越复杂,定位问题的难度也随之上升。不知为什
- 元素浮动导致的问题及解决办法大家都应该很熟悉了,举个简单的例子:<style type="text/css">
- 有时候我们只知道列的名字,但是不知道这列数据到底在哪个表里面,那么可以用下面的办法把含有这列数据的表查找出来。Select O.name o
- 生命游戏的算法就不多解释了,百度一下介绍随处可见。因为网上大多数版本都是基于pygame,matlab等外部库实现的,二维数组大多是用num
- 在使用flask部署Keras,tensorflow等框架时候,经常出现FailedPreconditionError: Attemptin
- 1、打开文件open()函数简介 :打开文件使用open函数,可以打开一个已经存在的文件,如果没有这个文件的话,会创建一个新文件完整的语法格
- 文字链接可以说是网页中最常见的页面元素了,默认的文字链接样式都是带下划线的效果,这种一陈不变的外观可能使很多朋友都想改变它,以使之符合页面的