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中通过ADO调用Asscess数据库和COM程序
Oct 09 PHP
php面向对象全攻略 (二) 实例化对象 使用对象成员
Sep 30 PHP
php算开始时间到过期时间的相隔的天数
Jan 12 PHP
php并发对MYSQL造成压力的解决方法
Feb 21 PHP
PHP 转义使用详解
Jul 15 PHP
PHP小技巧之JS和CSS优化工具Minify的使用方法
May 19 PHP
用php守护另一个php进程的例子
Feb 13 PHP
CI框架的安全性分析
May 18 PHP
php利用ffmpeg提取视频中音频与视频画面的方法详解
Jun 07 PHP
PHP递归实现文件夹的复制、删除、查看大小操作示例
Aug 11 PHP
PHP设计模式之工厂模式定义与用法详解
Apr 03 PHP
基于Laravel-admin 后台的自定义页面用法详解
Sep 30 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
全国FM电台频率大全 - 1 北京市
2020/03/11 无线电
一首老MP3,致敬WAR3经典
2021/03/08 魔兽争霸
php 归并排序 数组交集
2011/05/10 PHP
php中echo()和print()、require()和include()等易混淆函数的区别
2012/02/22 PHP
PHP验证终端类型是否为手机的简单实例
2017/02/07 PHP
ThinkPHP框架使用redirect实现页面重定向的方法实例分析
2018/04/12 PHP
javascript下查找父节点的简单方法
2007/08/13 Javascript
JavaScript获取当前页面上的指定对象示例代码
2014/02/28 Javascript
Jquery焦点图实例代码
2014/11/25 Javascript
avalon js实现仿google plus图片多张拖动排序附源码下载
2015/09/24 Javascript
深入对Vue.js $watch方法的理解
2017/03/20 Javascript
实例详解JavaScript中setTimeout函数的执行顺序
2017/07/12 Javascript
vue.js模仿京东省市区三级联动的选择组件实例代码
2017/11/22 Javascript
浅谈Koa2框架利用CORS完成跨域ajax请求
2018/03/06 Javascript
JS数组实现分类统计实例代码
2018/09/30 Javascript
解决vue2 在mounted函数无法获取prop中的变量问题
2018/11/15 Javascript
深入解析koa之异步回调处理
2019/06/17 Javascript
layui 地区三级联动 form select 渲染的实例
2019/09/27 Javascript
解决layui页面按钮点击无反应,也不报错的问题
2019/09/29 Javascript
小程序实现上传视频功能
2020/08/18 Javascript
Python随机读取文件实现实例
2017/05/25 Python
python使用标准库根据进程名如何获取进程的pid详解
2017/10/31 Python
python批量设置多个Excel文件页眉页脚的脚本
2018/03/14 Python
python五子棋游戏的设计与实现
2019/06/18 Python
妙用itchat! python实现久坐提醒功能
2019/11/25 Python
pyspark 随机森林的实现
2020/04/24 Python
Python使用re模块验证危险字符
2020/05/21 Python
python 使用paramiko模块进行封装,远程操作linux主机的示例代码
2020/12/03 Python
用HTML5.0制作网页的教程
2010/05/30 HTML / CSS
中软Java笔试题
2012/11/11 面试题
大学生旷课检讨书
2014/01/22 职场文书
初三学生个人自我评定
2014/04/06 职场文书
1亿有多大教学反思
2014/05/01 职场文书
公司备用金管理制度
2015/08/04 职场文书
初中政治教学反思
2016/02/23 职场文书
mysql全面解析json/数组
2022/07/07 MySQL