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判断输入不超过mysql的varchar字段的长度范围
Jun 24 PHP
php安全配置 如何配置使其更安全
Dec 16 PHP
php全排列递归算法代码
Oct 09 PHP
PHP之生成GIF动画的实现方法
Jun 07 PHP
phpQuery占用内存过多的处理方法
Nov 13 PHP
php实现获取局域网所有用户的电脑IP和主机名、及mac地址完整实例
Jul 18 PHP
FastCGI 进程意外退出造成500错误
Jul 26 PHP
简单介绍PHP非阻塞模式
Mar 03 PHP
一段实用的php验证码函数
May 19 PHP
php变量与JS变量实现不通过跳转直接交互的方法
Aug 25 PHP
php tpl模板引擎定义与使用示例
Aug 09 PHP
PHP生成随机密码4种方法及性能对比
Dec 11 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数组函数序列之array_search()- 按元素值返回键名
2011/11/04 PHP
跟我学Laravel之安装Laravel
2014/10/15 PHP
php类中的$this,static,final,const,self这几个关键字使用方法
2015/12/14 PHP
AJAX PHP无刷新form表单提交的简单实现(推荐)
2016/09/09 PHP
php实现对短信验证码发送次数的限制实例讲解
2021/03/04 PHP
javascript模拟的Ping效果代码 (Web Ping)
2011/03/13 Javascript
关于页面嵌入swf覆盖div层的问题的解决方法
2014/02/11 Javascript
JavaScript变量的作用域全解析
2015/08/14 Javascript
ionic 上拉菜单(ActionSheet)实例代码
2016/06/06 Javascript
详解Node.js开发中的express-session
2017/05/19 Javascript
Swiper实现轮播图效果
2017/07/03 Javascript
vue与vue-i18n结合实现后台数据的多语言切换方法
2018/03/08 Javascript
vue文件树组件使用详解
2018/03/29 Javascript
对angular2中的ngfor和ngif指令嵌套实例讲解
2018/09/12 Javascript
基于redis的小程序登录实现方法流程分析
2020/05/25 Javascript
深入剖析Python的爬虫框架Scrapy的结构与运作流程
2016/01/20 Python
Python实现的HMacMD5加密算法示例
2018/04/03 Python
Python切片操作深入详解
2018/07/27 Python
在python里从协程返回一个值的示例
2019/02/19 Python
django-rest-framework 自定义swagger过程详解
2019/07/18 Python
pandas DataFrame 警告(SettingWithCopyWarning)的解决
2019/07/23 Python
python函数局部变量、全局变量、递归知识点总结
2019/11/15 Python
详解基于Jupyter notebooks采用sklearn库实现多元回归方程编程
2020/03/25 Python
500行python代码实现飞机大战
2020/04/24 Python
美国二手奢侈品寄售网站:TheRealReal
2016/10/29 全球购物
Mamaearth官方网站:印度母婴护理产品公司
2019/10/06 全球购物
药学专业大专生的自我评价
2013/12/12 职场文书
一句话工作感言
2014/03/01 职场文书
《东方明珠》教学反思
2014/04/20 职场文书
安全生产计划书
2014/05/04 职场文书
机关领导干部作风整顿整改措施
2014/09/19 职场文书
大学生村官个人对照检查材料(群众路线)
2014/09/26 职场文书
民主评议教师党员自我评价
2015/03/04 职场文书
JavaScript函数柯里化
2021/11/07 Javascript
Mysql排查分析慢sql之explain实战案例
2022/04/19 MySQL
MySQL使用IF语句及用case语句对条件并结果进行判断 
2022/09/23 MySQL