CodeIgniter框架验证码类库文件与用法示例


Posted in PHP onMarch 18, 2017

本文实例讲述了CodeIgniter框架验证码类库文件与用法。分享给大家供大家参考,具体如下:

折腾了我四五个小时,终于,ci的验证码类库成功的整出来了。

下面请看源码:

在application/libraries建立Authcode.php文件,代码如下:

<?php
class Authcode
{
 var $CI;
 var $fontPath;//字体路径
 var $image;
 var $charLen   = 4; //生成几位验证码
 var $arrChr   = array();//验证码字符
 var $width    = 83; //图片宽
 var $height   = 24; //图片高
 var $bgcolor   = "#ffffff"; //背景色
 var $showNoisePix  = true; //生成杂点
 var $noiseNumPix  = 80; //生成杂点数量
 var $showNoiseLine  = true; //生成杂线
 var $noiseNumLine  = 2; //生成杂线数量
 var $showBorder  = true; //边框,当杂点、线一起作用的时候,边框容易受干扰
 var $borderColor  = "#000000";
 function Authcode()
 {
  $this->CI = & get_instance();
  $this->fontPath = realpath(dirname(__FILE__) . '/fonts/'); //字体文件
  //$this->arrChr   = array_merge(range(1, 9) , range('A', 'Z'));//数字字母验证码
  //$this->arrChr   = range('A', 'Z');//纯字母验证码
  $this->arrChr = range(0, 9);//纯数字验证码
 }
 /**
  * 显示验证码
  *
  */
 function show()
 {
  $this->image = imageCreate($this->width, $this->height);
  $this->back = $this->getColor($this->bgcolor);
  imageFilledRectangle($this->image, 0, 0, $this->width, $this->height, $this->back);
  $size = $this->width / $this->charLen - 4;
  if ($size > $this->height) {
   $size = $this->height;
  }
  $left = ($this->width - $this->charLen * ($size + $size / 10)) / $size + 5;
  $code = '';
  for($i = 0; $i < $this->charLen; $i ++) {
   $randKey = rand(0, count($this->arrChr) - 1);
   $randText = $this->arrChr[$randKey];
   $code .= $randText;
   $textColor = imageColorAllocate($this->image, rand(0, 100), rand(0, 100), rand(0, 100));
   $font = $this->fontPath . '/' . rand(1, 5) . ".ttf";
   $randsize = rand($size - $size / 10, $size + $size / 10);
   $location = $left + ($i * $size + $size / 10);
   @imagettftext($this->image, $randsize, rand(- 18, 18), $location, rand($size - $size / 10, $size + $size / 10) + 2, $textColor, $font, $randText);
  }
  if ($this->showNoisePix == true) {
   $this->setNoisePix();
  }
  if ($this->showNoiseLine == true) {
   $this->setNoiseLine();
  }
  if ($this->showBorder == true) {
   $this->borderColor = $this->getColor($this->borderColor);
   imageRectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $this->borderColor);
  }
  $this->CI->session->set_userdata('auth_code', $code);
  ob_clean();
  header("Content-type: image/jpeg");
  imagejpeg($this->image);
  imagedestroy($this->image);
 }
 /**
  * 显示验证码的JS调用
  *
  */
 function showScript()
 {
  //显示验证码
  echo "var img_src = '/imgauthcode/show/?';\n";
  echo "document.writeln('<img id=\"img_authcode\" src=\"' + img_src + Math.random() + '\" style=\"cursor:hand;\" onclick=\"this.src=img_src + Math.random();\" alt=\"点击更换图片\">');";
 }
 /**
  * 检查验证码是否正确
  *
  * @param string $auth_code
  * @return bool
  */
 function check($auth_code = null)
 {
  return ($this->CI->session->userdata('auth_code') && $auth_code) ? ($this->CI->session->userdata('auth_code') === $auth_code) : false;
 }
 function getColor($color)
 {
  $color = eregi_replace("^#", "", $color);
  $r = $color[0] . $color[1];
  $r = hexdec($r);
  $b = $color[2] . $color[3];
  $b = hexdec($b);
  $g = $color[4] . $color[5];
  $g = hexdec($g);
  $color = imagecolorallocate($this->image, $r, $b, $g);
  return $color;
 }
 function setNoisePix()
 {
  for($i = 0; $i < $this->noiseNumPix; $i ++) {
   $randColor = imageColorAllocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
   imageSetPixel($this->image, rand(0, $this->width), rand(0, $this->height), $randColor);
  }
 }
 function setNoiseLine()
 {
  for($i = 0; $i < $this->noiseNumLine; $i ++) {
   $randColor = imageColorAllocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
   imageline($this->image, rand(1, $this->width), rand(1, $this->height), rand(1, $this->width), rand(1, $this->height), $randColor);
  }
 }
}

Authcode.php代码结束

在Controller中,有个admin类,其中有两个方法:

Class Admin extends CI_Controller{
 function __construct()
 {
  parent::__construct();
  $this->load->library('Authcode');
 }
function captcha(){
  if($_POST){
    if ($this->authcode->check($this->input->post('gd_pic'))) {
    echo "right";
   } else {
    echo '验证码不正确,请重新输入';
   }
  }else{
   $this->load->view('demo');
  }
 }
 function show_captcha(){ //此方法用于显示验证码图片,归一个view中的img的src调用
  $this->authcode->show();
 }
}

下面是在视图view中创建一个demo.php了,代码如下:

<?php echo form_open('c=admin&m=captcha');?>
<input type="text" name="gd_pic" />
<img src="<?php echo base_url('?c=admin&m=show_captcha');?>" ><br>
<input type="submit" name="submit" value="验证" />
<?php echo form_close();?>

OK. 一切结束,终于正常运行了。

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

PHP 相关文章推荐
NO3第三帝国留言簿制作过程
Oct 09 PHP
基于php iconv函数的使用详解
Jun 09 PHP
PHP多例模式介绍
Jun 24 PHP
php统计时间和内存使用情况示例分享
Mar 13 PHP
php实现斐波那契数列的简单写法
Jul 19 PHP
yii的CURD操作实例详解
Dec 04 PHP
隐性调用php程序的方法
Jun 13 PHP
PHP数组游标实现对数组的各种操作详解
Jan 26 PHP
Yii2 输出xml格式数据的方法
May 03 PHP
分享一个漂亮的php验证码类
Sep 29 PHP
PHP中的日期时间处理利器实例(Carbon)
Jun 09 PHP
laravel实现图片上传预览,及编辑时可更换图片,并实时变化的例子
Nov 14 PHP
YII框架批量插入数据的方法
Mar 18 #PHP
thinkPHP5.0框架URL访问方法详解
Mar 18 #PHP
thinkPHP5.0框架模块设计详解
Mar 18 #PHP
thinkPHP5.0框架命名空间详解
Mar 18 #PHP
thinkPHP5.0框架自动加载机制分析
Mar 18 #PHP
thinkPHP5.0框架引入Traits功能实例分析
Mar 18 #PHP
2017年最新PHP经典面试题目汇总(上篇)
Mar 17 #PHP
You might like
php在线解压ZIP文件的方法
2014/12/30 PHP
PHP多进程编程总结(推荐)
2016/07/18 PHP
PHP实现四种基础排序算法的运行时间比较(推荐)
2016/08/11 PHP
PHP入门教程之面向对象的特性分析(继承,多态,接口,抽象类,抽象方法等)
2016/09/11 PHP
laravel框架邮箱认证实现方法详解
2019/11/22 PHP
JavaScript 对象成员的可见性说明
2009/10/16 Javascript
javascript限制文本框只允许输入数字(曾经与现在的方法对比)
2013/01/18 Javascript
使用javascript实现ListBox左右全选,单选,多选,全请
2013/11/07 Javascript
js 模式窗口(模式对话框和非模式对话框)的使用介绍
2014/07/17 Javascript
基于JS判断iframe是否加载成功的方法(多种浏览器)
2016/05/13 Javascript
JavaScript 计算笛卡尔积实例详解
2016/12/02 Javascript
angularjs+bootstrap菜单的使用示例代码
2017/03/07 Javascript
React.js中常用的ES6写法总结(推荐)
2017/05/09 Javascript
ReactNative实现图片上传功能的示例代码
2017/07/11 Javascript
在 Angular中 使用 Lodash 的方法
2018/02/11 Javascript
微信小程序内拖动图片实现移动、放大、旋转的方法
2018/09/04 Javascript
react的滑动图片验证码组件的示例代码
2019/02/27 Javascript
Layui带搜索的下拉框的使用以及动态数据绑定方法
2019/09/28 Javascript
python虚拟环境 virtualenv的简单使用
2020/01/21 Javascript
JavaScript本地储存:localStorage、sessionStorage、cookie的使用
2020/10/13 Javascript
[56:00]DOTA2上海特级锦标赛主赛事日 - 4 胜者组决赛Secret VS Liquid第一局
2016/03/05 DOTA
Python迭代用法实例教程
2014/09/08 Python
教你用Python创建微信聊天机器人
2020/03/31 Python
解决python3 pika之连接断开的问题
2018/12/18 Python
python如何使用代码运行助手
2020/07/03 Python
Python实现壁纸下载与轮换
2020/10/19 Python
Python大批量搜索引擎图像爬虫工具详解
2020/11/16 Python
Lululemon英国官网:加拿大瑜伽服装品牌
2019/01/14 全球购物
党委书记岗位职责
2013/11/24 职场文书
乡村卫生服务一体化管理实施方案
2014/03/30 职场文书
生日宴会策划方案
2014/06/03 职场文书
销售目标责任书
2014/07/23 职场文书
聚众斗殴罪辩护词
2015/05/21 职场文书
士兵突击观后感
2015/06/16 职场文书
2016年暑假学生家长评语
2015/12/01 职场文书
Python机器学习之基础概述
2021/05/19 Python