ci框架中,扩展验证码类。

Yxh_blogs Yxh_blogs     2022-08-24     419

关键词:

使用CI框架的朋友,应该都知道CI框架的的验证码辅助函数,不太好用。它需要写入到数据库中,然后再进行比对。

大家在实际项目中,好像不会这样去使用,因为会对数据库造成一定的压力。

所以,我们还是利用session来临时存储验证码,比较的稳妥。

下面附上验证码类的代码。这个类是放在libraries这个库文件夹下。

<?php

/**
 * 验证码类
 */

class Code{
    //资源
    private $img;
    //画布宽度
    public $width = 150;
    //画布高度
    public $height = 45;
    //背景颜色
    public $bgColor = "#ffffff";
    //验证码
    public $code;
    //验证码的随机种子
    public $codeStr = "123456789abcdefghijklmnpqrstuvwsyz";
    //验证码长度
    public $codeLen = 4;
    //验证码字体
    public $font = "";//具体环境具体需要更改路径
    //验证码字体大小
    public $fontSize = 22;
    //验证码字体颜色
    public $fontColor = "";

    /**
     * 构造函数
     */
    public function __construct($arr = array()) {

        $width = '';
        $height = '';
        $codeLen = '';
        $fontSize = '';
        $bgColor = '';
        $fontColor = '';

        if(!empty($arr)){
           extract($arr); 
        } 
        $this->font = BASEPATH . "fonts/font.ttf";       
        if (!is_file($this->font)) {
            error("验证码字体文件不存在");
        }
        $this->width = empty($width) ? $this->width : $width;
        $this->height = empty($height) ? $this->height : $height;
        $this->bgColor = empty($bgColor) ? $this->bgColor : $bgColor;
        $this->codeLen = empty($codeLen) ? $this->codeLen : $codeLen;
        $this->fontSize = empty($fontSize) ? $this->fontSize : $fontSize;
        $this->fontColor = empty($fontColor) ? $this->fontColor : $fontColor;
        $this->create();//生成验证码
    }

    /**
     * 生成验证码
     */
    private function createCode() {
        $code = '';
        for ($i = 0; $i < $this->codeLen; $i++) {
            $code .= $this->codeStr [mt_rand(0, strlen($this->codeStr) - 1)];
        }
        $this->code = strtoupper($code);
        if(!isset($_SESSION)){
            session_start();
        }
        $_SESSION ['code'] = $this->code;
    }

    /**
     * 返回验证码
     */
    public function getCode() {
        return $this->code;
    }

    /**
     * 建画布
     */
    public function create() {
        if (!$this->checkGD())
            return false;
        $w = $this->width;
        $h = $this->height;
        $bgColor = $this->bgColor;
        $img = imagecreatetruecolor($w, $h);
        $bgColor = imagecolorallocate($img, hexdec(substr($bgColor, 1, 2)), hexdec(substr($bgColor, 3, 2)), hexdec(substr($bgColor, 5, 2)));
        imagefill($img, 0, 0, $bgColor);
        $this->img = $img;
        $this->createLine();
        $this->createFont();
        $this->createPix();
        $this->createRec();
    }
    /**
    *  画线
    */
    private function createLine(){
        $w = $this->width;
        $h = $this->height;
        $line_height = $h/10;
        $line_color = "#D0D0D0";
        $color = imagecolorallocate($this->img, hexdec(substr($line_color, 1, 2)), hexdec(substr($line_color, 3, 2)), hexdec(substr($line_color, 5, 2)));
        for($i=0;$i<10;$i++){
            $step =$line_height*$i+2;
            imageline($this->img, 0, $step, $w,$step, $color);
        }
        $line_width = $w/10;
        for($i=0;$i<10;$i++){
            $step =$line_width*$i+2;
            imageline($this->img, $step-2, 0, $step+2,$h, $color);
        }
    }
    /**
     * 画矩形边框
     */
    private function createRec() {
        imagerectangle($this->img, 0, 0, $this->width - 1, $this->height - 1, $this->fontColor);
    }

    /**
     * 写入验证码文字
     */
    private function createFont() {
        $this->createCode();
        $color = $this->fontColor;
        if (!empty($color)) {
            $fontColor = imagecolorallocate($this->img, hexdec(substr($color, 1, 2)), hexdec(substr($color, 3, 2)), hexdec(substr($color, 5, 2)));
        }
        $x = ($this->width - 10) / $this->codeLen;
        for ($i = 0; $i < $this->codeLen; $i++) {
            if (empty($color)) {
                $fontColor = imagecolorallocate($this->img, mt_rand(50, 155), mt_rand(50, 155), mt_rand(50, 155));
            }
            imagettftext($this->img, $this->fontSize, mt_rand(- 30, 30), $x * $i + mt_rand(6, 10), mt_rand($this->height / 1.3, $this->height - 5), $fontColor, $this->font, $this->code [$i]);
        }
        $this->fontColor = $fontColor;
    }

    /**
     * 画线
     */
    private function createPix() {
        $pix_color = $this->fontColor;
        for ($i = 0; $i < 50; $i++) {
            imagesetpixel($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
        }

        for ($i = 0; $i < 2; $i++) {
            imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
        }
        //画圆弧
        for ($i = 0; $i < 1; $i++) {
            // 设置画线宽度
           // imagesetthickness($this->img, mt_rand(1, 3));
            imagearc($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height)
                    , mt_rand(0, 160), mt_rand(0, 200), $pix_color);
        }
        imagesetthickness($this->img, 1);
    }

    /**
     * 显示验证码
     */
    public function show() {
        header("Content-type:image/png");
        imagepng($this->img);
        imagedestroy($this->img);
        exit;
    }

    /**
     * 验证GD库是不否打开imagepng函数是否可用
     */
    private function checkGD() {
        return extension_loaded('gd') && function_exists("imagepng");
    }
}

  然后再控制器中调用就可以了。

最后,提醒大家记得开启在自动加载文件中session哦。

 

面向对象中的验证码类

<?php/*** 验证码类*/class Verify{ //成员属性 private $width; //宽 private $height; //高 private $verify_code; //验证码字符串 private $verify_nums; //验证码个数 private $verify_type 查看详情

php验证码类

通过PHP的GD库图像处理内容,设计一个验证码类Vcode。将该类声明在文件vcode.class.php中,并通过面向对象的特性将一些实现的细节封装在该类中。只要在创建对象时,为构造方法提供三个参数,包括创建验证码图片的宽度、高度... 查看详情

4-1验证码类封装之gd库验证(代码片段)

1<?php2/**3*GDBasic.php45*descriptionGD基础类6*/78namespaceImooc\Lib;91011classGDBasic1213protectedstatic$_check=false;1415//检查服务器环境中gd库16publicstaticfunctioncheck()1718//当静态变量不为false19if(static::$_ 查看详情

2载入验证码类及$_session处理

1、载入验证码类,并验证(1)下载定义好的code验证码类,放置到resources目录下 (2)添加路由Route::get(‘/admin/code‘,‘Admin[email protected]‘);(3)添加方法LoginController.phppublicfunctioncode(){$code=newCode();$code->make();}注意: 查看详情

ci框架,参数验证

/***统一API参数检验方法**调用示例check_param(array(‘money‘=>array(‘required‘,‘integer‘,‘greater_than_equal_to[1]‘,‘less_than_equal_to[200]‘)));**@accesspublic*@paramarray$arr*@since1.0*@returnboolean*/publicfu 查看详情

4-5验证码类封装(代码片段)

1.Captcha.php1<?php2/**3*Captcha.php45*description验证码类6*/78namespaceImooc\Lib;910require_once‘GDBasic.php‘;1112classCaptchaextendsGDBasic1314//图像宽度15protected$_width=60;16//图像高度17protected$_height 查看详情

建立一个漂亮的php验证码类文件及调用方式

//验证码类classValidateCode{ private$charset=‘abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789‘;//随机因子 private$code;//验证码 private$codelen=4;//验证码长度 private$width=130;//宽度 privat 查看详情

包装一个php的验证码类

  验证码是我们开发的时候经常用到的功能,所以在此本人包装了一个验证码类,应该可以作为php的类插件用,在此分享给各位博客园的读友。  实现的原理也是很简单,就是利用画布的几个元素,再加上一些字符串的获取... 查看详情

后盾网-ci框架学习笔记

CI框架:  表单验证:    载入验证类      $this->load->library(‘form_validation‘);    设置规则      $this->form_validation->set_rules(‘name值‘,‘规则‘);    执行验证      $this->form_valida... 查看详情

ci框架2.x的验证码中所遇问题解决

用php版本是5.6,CI框架版本是2.x,在使用验证码(captcha)时,遇到一些问题。首先,我查看框架手册,说必需的两个参数是"img_url",“img_path”,其他的参数都可以省略,但是,我在使用的时候,如果只指定这两个参数,一运行就... 查看详情

john细说php的验证码

细说php中的验证码类创建我这里自己写了一个验证码类,我来演示一下怎么使用,我是菜鸟一枚,大神请略过。我来讲解一下它的使用方法,总共需要两步即可。第一步:下载我制作好的验证码类。下载地址:http://files.cnblogs.com... 查看详情

在codeigniter中,大家使用smarty吗?

...都是自己的成果,也可以共享出来。如果喜欢拿来就用的框架,那还是ZF比较合适,是吧? 参考技术B我已经扩展了Model/Controller层了,还要扩展Loader类?DB类也要自己扩展,发现CI不好玩了。。 参考技术Csmarty整合进CI,不知道算... 查看详情

ci中的验证码

    CI中的验证码相对来说使用非常方便直接加载类调用函数以及一些配置,代码如上,比较简单,具体函数可在CI手册的辅助函数参考CAPTCHA辅助函数中查询,CI中的验证码是直接生成验证码图片在你自己创建的CI根目录下(... 查看详情

后盾网-ci框架实例教程-马振宇-学习笔记

第四节视频:  表单验证操作:    1、载入验证类      $this->load->library(‘form_validation‘);    2、设置规则      $this->form_validation->set_rules(‘name值‘,‘标签名称’,‘规则’);    3、... 查看详情

dubbo3高级特性「框架与服务」在dubbo3中进行参数校验及自定义验证扩展机制(代码片段)

Dubbo3的参数验证机制参数验证功能是基于JSR303实现的,用户只需标识JSR303标准的验证annotation,并通过声明filter来实现验证。JSR303https://jcp.org/en/jsr/detail?id=303Maven依赖<dependency><groupId>javax.validation</groupId><artif 查看详情

ci框架手册-阅读笔记

ci框架手册来自:CodeIgniter中国官方手册+++++++++++++++++++++++++++++++++++++ 教程-内容提要:  --创建一个简单新闻系统  --教程主要介绍以下技术要点:    1、模型-视图-控制器(Model-View-Controller)基础知识    2、URI... 查看详情

详解struts中validator验证框架的使用

参考技术A  Validator框架已成为Jakarta的公共项目的一部分可以从下载单独的Validator框架在Struts中已经带了这个框架  Validator主要依赖两个jar包  Jakartaorojar-提供一组处理文本的类具有文本替换过滤和分割功能  Commonsvalid... 查看详情

求php图片验证码类给出详细调用方法谢谢!!!

...:yangjie_128@163.com参考技术A[code.php]<?php/** *  验证码图片 */session_start();Header("Content-type: image/gif");/** 初始化 */$border = 0; //是否要边框 1要:0不要$how = 4; //验证码位数$w&nbs... 查看详情