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 相关文章推荐
在mysql数据库原有字段后增加新内容
Nov 26 PHP
常见的PHP五种设计模式小结
Mar 23 PHP
五款常用mysql slow log分析工具的比较分析
May 22 PHP
php数据结构 算法(PHP描述) 简单选择排序 simple selection sort
Aug 09 PHP
使用php判断网页是否gzip压缩
Jun 25 PHP
使用PHP强制下载PDF文件示例
Jan 17 PHP
Yii使用smsto短信接口的函数demo示例
Jul 13 PHP
php7 安装yar 生成docker镜像
May 09 PHP
php微信公众号开发之欢迎老朋友
Oct 20 PHP
php中pcntl_fork创建子进程的方法实例
Mar 14 PHP
PHP检测一个数组有没有定义的方法步骤
Jul 20 PHP
PHP使用非对称加密算法RSA
Apr 21 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中的正则表达式以及模式匹配
2013/06/19 PHP
ThinkPHP3.1新特性之内容解析输出详解
2014/06/19 PHP
CentOS7编译安装php7.1的教程详解
2019/04/18 PHP
javascript web页面刷新的方法收集
2009/07/02 Javascript
JavaScript对象创建及继承原理实例解剖
2013/02/28 Javascript
event对象获取方法总结在google浏览器下测试
2013/11/03 Javascript
JS的参数传递示例介绍
2014/02/08 Javascript
javascript关于运动的各种问题经典总结
2015/04/27 Javascript
浅谈JavaScript中指针和地址
2015/07/26 Javascript
jquery validate表单验证的基本用法入门
2016/01/18 Javascript
javascript弹出窗口中增加确定取消按钮
2016/06/24 Javascript
Angularjs 实现一个幻灯片示例代码
2016/09/08 Javascript
Angularjs中controller的三种写法分享
2016/09/21 Javascript
详解jQuery简单的表单应用
2016/12/16 Javascript
微信小程序 JS动态修改样式的实现代码
2017/02/10 Javascript
javaScript封装的各种写法
2017/08/14 Javascript
JavaScript基于遍历操作实现对象深拷贝功能示例
2019/03/05 Javascript
Vue在chrome44偶现点击子元素事件无法冒泡的解决方法
2019/12/15 Javascript
使用PyV8在Python爬虫中执行js代码
2017/02/16 Python
python @classmethod 的使用场合详解
2019/08/23 Python
Python3的socket使用方法详解
2020/02/18 Python
python序列类型种类详解
2020/02/26 Python
python 爬取英雄联盟皮肤并下载的示例
2020/12/04 Python
css3绘制百度的小度熊
2018/10/29 HTML / CSS
全球烹饪课程的领先预订平台:Cookly
2020/01/28 全球购物
什么是TCP/IP
2014/07/27 面试题
公司请假条范文
2014/04/11 职场文书
高一学生评语大全
2014/04/25 职场文书
学校开学标语
2014/10/06 职场文书
市场营销计划书范文
2015/01/16 职场文书
本科毕业论文致谢怎么写
2015/05/14 职场文书
2015年学校信息技术工作总结
2015/05/25 职场文书
美容院管理规章制度
2015/08/05 职场文书
Python使用Kubernetes API访问集群
2021/05/30 Python
Nginx+Tomcat负载均衡集群的实现示例
2021/10/24 Servers
Tomcat 与 maven 的安装与使用教程
2022/06/16 Servers