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 相关文章推荐
php数组对百万数据进行排除重复数据的实现代码
Jun 08 PHP
PHP 函数执行效率的小比较
Oct 17 PHP
PHP输出数组中重名的元素的几种处理方法
Sep 05 PHP
Mysql中分页查询的两个解决方法比较
May 02 PHP
使用php实现下载生成某链接快捷方式的解决方法
May 07 PHP
php多文件上传下载示例分享
Feb 20 PHP
destoon实现会员商铺中指定会员或会员组投放广告的方法
Aug 21 PHP
php $_SESSION会员登录实例分享
Jan 19 PHP
既简单又安全的PHP验证码 附调用方法
Jun 02 PHP
php微信公众号开发(2)百度BAE搭建和数据库使用
Dec 15 PHP
thinkphp 字母函数详解T/I/N/D/M/A/R/U
Apr 03 PHP
laravel model模型定义实现开启自动管理时间created_at,updated_at
Oct 17 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加速 eAccelerator配置和使用指南
2009/06/05 PHP
php smarty 二级分类代码和模版循环例子
2011/06/01 PHP
javascript 常用方法总结
2009/06/03 Javascript
js 与或运算符 || &amp;&amp; 妙用
2009/12/09 Javascript
JavaScript 匿名函数(anonymous function)与闭包(closure)
2011/10/04 Javascript
Js 代码中,ajax请求地址后加随机数防止浏览器缓存的原因
2013/05/07 Javascript
jquery异步跨域访问代码
2013/06/28 Javascript
给js文件传参数(详解)
2014/07/13 Javascript
JavaScript实现cookie的写入、读取、删除功能
2015/11/05 Javascript
基于jquery实现图片相关操作(重绘、获取尺寸、调整大小、缩放)
2015/12/25 Javascript
Bootstrap树形菜单插件TreeView.js使用方法详解
2016/11/01 Javascript
详解Node.js 命令行程序开发教程
2017/06/07 Javascript
JavaScript折半查找(二分查找)算法原理与实现方法示例
2018/08/06 Javascript
vue项目动态设置页面title及是否缓存页面的问题
2018/11/08 Javascript
JS栈stack类的实现与使用方法示例
2019/01/31 Javascript
vue中使用elementUI组件手动上传图片功能
2019/12/13 Javascript
Python获取网页上图片下载地址的方法
2015/03/11 Python
Python二叉搜索树与双向链表转换实现方法
2016/04/29 Python
python导入时小括号大作用
2017/01/10 Python
Pandas数据离散化原理及实例解析
2019/11/16 Python
python3.6连接mysql数据库及增删改查操作详解
2020/02/10 Python
Django限制API访问频率常用方法解析
2020/10/12 Python
Python之多进程与多线程的使用
2021/02/23 Python
css3实现小箭头各种图形效果
2020/07/08 HTML / CSS
施华洛世奇加拿大官网:SWAROVSKI加拿大
2018/06/03 全球购物
车工岗位职责
2013/11/26 职场文书
总经理驾驶员岗位职责
2013/12/04 职场文书
女子职高个人自荐书
2014/02/01 职场文书
三方协议书范本
2014/04/22 职场文书
2014党员民主评议个人总结
2014/09/10 职场文书
家庭贫困证明书(3篇)
2014/09/15 职场文书
幼儿园安全教育月活动总结
2015/05/08 职场文书
python3读取文件指定行的三种方法
2021/05/24 Python
Python机器学习应用之基于线性判别模型的分类篇详解
2022/01/18 Python
Redis Lua脚本实现ip限流示例
2022/07/15 Redis
win11怎么消除图标小盾牌?win11消除图标小盾牌解决方法
2022/08/05 数码科技