php生成图片验证码的实例讲解


Posted in PHP onAugust 03, 2015

本文以实例演示5种验证码,并介绍生成验证码的函数。PHP生成验证码的原理:通过GD库,生成一张带验证码的图片,并将验证码保存在Session中。

php生成图片验证码的实例讲解

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);//释放图片所占内存
}

以上就是本文的全部内容,希望对大家的学习有所帮助。

PHP 相关文章推荐
PHP的一个完整SMTP类(解决邮件服务器需要验证时的问题)
Oct 09 PHP
利用PHP创建动态图像
Oct 09 PHP
php 各种应用乱码问题的解决方法
May 09 PHP
PHP对字符串的递增运算分析
Aug 08 PHP
解决PHP在DOS命令行下却无法链接MySQL的技术笔记
Dec 29 PHP
php json_encode奇怪问题说明
Sep 27 PHP
简单的php数据库操作类代码(增,删,改,查)
Apr 08 PHP
解析wamp5下虚拟机配置文档
Jun 27 PHP
php实现httpclient类示例
Apr 08 PHP
PHP实现将视频转成MP4并获取视频预览图的方法
Mar 12 PHP
ThinkPHP路由机制简介
Mar 23 PHP
laravel5环境隐藏index.php后缀(apache)的方法
Oct 12 PHP
android上传图片到PHP的过程详解
Aug 03 #PHP
php将远程图片保存到本地服务器的实现代码
Aug 03 #PHP
php基于session实现数据库交互的类实例
Aug 03 #PHP
php通过排列组合实现1到9数字相加都等于20的方法
Aug 03 #PHP
PHP实现递归复制整个文件夹的类实例
Aug 03 #PHP
UTF-8正则表达式如何匹配汉字
Aug 03 #PHP
PHP使用缓存即时输出内容(output buffering)的方法
Aug 03 #PHP
You might like
PHP中的串行化变量和序列化对象
2006/09/05 PHP
克隆一个新项目的快捷方式
2013/04/10 PHP
常用PHP框架功能对照表
2014/10/23 PHP
Yii2汉字转拼音类的实例代码
2017/04/18 PHP
Yii2框架实现登陆添加验证码功能示例
2018/07/12 PHP
input的focus方法使用
2010/03/13 Javascript
jquery.AutoComplete.js中文修正版(支持firefox)
2010/04/09 Javascript
基于jQuery的图片左右无缝滚动插件
2012/05/23 Javascript
jquery统计复选框选中示例
2013/11/05 Javascript
Js保留小数点的4种效果实现代码分享
2014/04/12 Javascript
js函数模拟显示桌面.scf程序示例
2014/04/20 Javascript
js面向对象编程之如何实现方法重载
2014/07/02 Javascript
jquery用offset()方法获得元素的xy坐标
2014/09/06 Javascript
AngularJS控制器之间的数据共享及通信详解
2016/08/01 Javascript
基于JS如何实现给字符加千分符(65,541,694,158)
2016/08/03 Javascript
基于js对象,操作属性、方法详解
2016/08/11 Javascript
JS+CSS3制作炫酷的弹窗效果
2016/11/08 Javascript
element vue Array数组和Map对象的添加与删除操作
2018/11/14 Javascript
nodejs+express最简易的连接数据库的方法
2020/12/23 NodeJs
理解python多线程(python多线程简明教程)
2014/06/09 Python
python网络编程调用recv函数完整接收数据的三种方法
2017/03/31 Python
python简单商城购物车实例代码
2018/03/15 Python
python版本单链表实现代码
2018/09/28 Python
python opencv调用笔记本摄像头
2019/08/28 Python
Python利用多线程同步锁实现多窗口订票系统(推荐)
2019/12/22 Python
HTML5实现视频直播功能思路详解
2017/11/16 HTML / CSS
Etam俄罗斯:法国女士内衣和家居服网上商店
2019/10/30 全球购物
The Outnet亚太地区:折扣设计师时装店
2019/12/05 全球购物
利用异或运算实现两个无符号数的加法运算
2013/12/20 面试题
应用电子专业学生的自我评价
2013/10/16 职场文书
就业自荐书
2013/12/05 职场文书
学习经验交流会主持词
2014/04/01 职场文书
教师节标语大全
2014/10/07 职场文书
2014年幼儿园教学工作总结
2014/12/04 职场文书
迟到检讨书范文
2015/01/27 职场文书
导游词之千岛湖
2019/09/23 职场文书