codeigniter验证码类库

2023-05-16

http://hi.baidu.com/mediumgirl/item/c734b8f5a1cacfc3a835a2ae


折腾了我四五个小时,终于,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;\" οnclick=\"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();        
    }

}

————————————————————The End of The Class ————————————————————————

下面是在视图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();?>

 

————————————————————————————The End of The View——————————————————————

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

 

期待大家分享你们的代码。


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

codeigniter验证码类库 的相关文章

随机推荐

  • linux的nohup命令的用法

    http www cnblogs com allenblogs archive 2011 05 19 2051136 html 在应用Unix Linux时 xff0c 我们一般想让某个程序在后台运行 xff0c 于是我们将常会用 amp
  • Mysql中创建用户帐户的方法

    http www eygle com digest 2008 01 mysql create user html 1 CREATE USER CREATE USER user IDENTIFIED BY PASSWORD 39 passwo
  • 用mysql触发器自动更新memcache

    mysql 5 1支持触发器以及自定义函数接口 UDF 的特性 xff0c 如果配合libmemcache以及Memcached Functions for MySQL xff0c 就能够实现 memcache的自动更新 简单记录一下安装测
  • 一个很爽的前端网站,有大量的资源

    http www gbin1 com technology css index html firstentry 61 0
  • Html5 Geolocation获取地理位置信息

    http www cnblogs com lwbqqyumidi archive 2012 11 10 2764352 html Html5中提供了地理位置信息的API xff0c 通过浏览器来获取用户当前位置 基于此特性可以开发基于位置的
  • openfire整合外部数据库的方法

    http www igniterealtime org builds openfire docs latest documentation db integration guide html 看了这篇教程 xff0c 发现了一个问题 xff
  • 金融机构如何应对核心系统分布式智能化升级大潮?

    过去40多年 xff0c 中国金融业实现了技术上的引进 借鉴 xff0c 并逐渐开始进行原创性创新 比如 xff0c 在 支付系统建设方面 xff0c 我国现在就走在了世界的前列 从二代大小额支付系统CNAPS到跨境人民币支付系统CIPS再
  • ajax请求中session无效的问题

    遇到一个问题 xff0c 发现网站中的所有ajax在某个服务器中的session总是无效 xff0c 后来同事查了资料 xff0c 原来php的配置文件中有个选项 xff1a Whether or not to add the httpOn
  • 解决seesion在二级域名下无效的问题

    开发中遇到了一个问题 xff0c 当用户在www aa com登陆了 xff0c 在二级域名下的登陆无效 例如 aa com 后来检查了很久 xff0c 终于知道了问题所在 xff0c 在www aa com下生成的cookie不适用于 a
  • 提供全球商家信息的网站

    做LBS的应用 xff0c 商家信息的获取和维护是个很重要的问题 xff0c 在中国的某些大型网站是雇佣了兼职人员去维护这些数据 xff0c 但对于小公司来说这种方法是不现实的 现在发现了一个网站 xff0c 提供了全球的商家信息 xff0
  • 使用web端连接xmpp

    在apache的配置文件中加入下面3句 xff1a ProxyRequests Off ProxyPass xmpp httpbind http 127 0 0 1 7070 http bind ProxyPassReverse xmpp
  • ubuntu apache开启重写模块

    http www iblue cc 2011 09 ubuntu apache E5 BC 80 E5 90 AF E9 87 8D E5 86 99 E6 A8 A1 E5 9D 97 Ubuntu下apache2的rewrite模块默认
  • openfire xmpp 如何判断用户是否在线

    http iammr 7 blog 163 com blog static 49102699201041961613109 想象中如此简单的功能 xff0c 想不到却这般大费周折 如要实现 xff0c 必须先确保 xff1a 1 openf
  • sql 分组统计

    原始的数据结构是这样的 xff1a 这是一个信息表 xff0c 记录下每个app每天对应什么等级 现在需求是 xff1a 统计每天每个等级有多少个app xff1f 实现的sql如下 xff1a select count as num le
  • Errors running builder JavaScript Validator的问题

    http jc dreaming iteye com blog 1038995 最近使用eclipse时 xff0c 在编译项目总是出现问题 Errors occurred during the build Errors running b
  • coreseek索引更新机制

    k索引更新机制 版权声明 xff1a 转载时请以超链接形式标明文章原始出处和作者信息及本声明 http fatal blogbus com logs 45153968 html 61 61 xff0c 昨晚太晚睡觉 xff0c 所以日记又没
  • golang生成自定义标签名(带CDATA标识)的xml

    在golang中 xff0c 有时候需要生成带CDATA标识的xml值 xff0c 例如这种 xff1a lt xml version 61 34 1 0 34 gt lt xml gt lt to User gt lt CDATA use
  • 有人痴狂,有人跑路,开源软件新一年的冰火两重天

    最近有关开源软件的话题始终占领着IT界的新闻头条 xff0c Log4j开源软件的惊天漏洞 xff0c 才刚刚出现不久 xff0c Fake js的作者也惊天删库跑路了 xff0c CurL的作者怒怼苹果只会白嫖开源却不出力 xff0c L
  • linux下通过ssh用户名密码的rsync传输文件方法

    一般用rsync传输文件都会使用密钥的方式实现免密码验证 xff0c 但有些机器由于特殊的原因 xff0c 不能配置密钥 xff0c 这时就要用ssh的用户名和密码方式使用rsync 1 首先 xff0c 通过ssh 命令登录一次远程的主机
  • codeigniter验证码类库

    http hi baidu com mediumgirl item c734b8f5a1cacfc3a835a2ae 折腾了我四五个小时 xff0c 终于 xff0c ci的验证码类库成功的整出来了 下面请看源码 xff1a 在applic