wecenter学习笔记-验证码管理

该文是wecenter学习笔记的一部分

验证码管理

验证需要考虑

  • 验证码图片的生成、存储和访问
  • 验证码的有效期管理
  • 失效验证码的清理策略
  • 验证有效性

基于 Zend_Captcha_Image 实现的验证码生成和管理功能,可以根据自己提供的字体文件生成自定义大小和位数的验证码,
并存储到单独的session命名空间中。

system/core/captcha.php#__construct

$this->captcha = new Zend_Captcha_Image(array(
    'font' => $this->get_font(),
    'imgdir' => $img_dir,
    'fontsize' => rand(20, 22),
    'width' => 100,
    'height' => 40,
    'wordlen' => 4,
    'session' => new Zend_Session_Namespace(G_COOKIE_PREFIX . '_Captcha'),
    'timeout' => 600
));

$this->captcha->setDotNoiseLevel(rand(3, 6));
$this->captcha->setLineNoiseLevel(rand(1, 2));

** 使用方式和实现原理 **

生成验证码

AWS_APP::captcha()->generate();

system/core/captcha.php#generate

public function generate()
{
    $this->captcha->generate();

    HTTP::no_cache_header();

    readfile($this->captcha->getImgDir() . $this->captcha->getId() . $this->captcha->getSuffix());

    die;
}

检查验证码

if (!AWS_APP::captcha()->is_validate($_POST['seccode_verify']) AND get_setting('register_seccode') == 'Y')
{
    H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('请填写正确的验证码')));
}

system/core/captcha.php#is_validate

public function is_validate($validate_code, $generate_new = true)
{
    if (strtolower($this->captcha->getWord()) == strtolower($validate_code))
    {
        if ($generate_new)
        {
            $this->captcha->generate();
        }
        
        return true;
    }

    return false;
}

Zend_Captcha_Image

** 生成验证码图片 **

Zend/Captcha/Image.php

public function generate()
{
    $id = parent::generate();
    $tries = 5;
    // If there's already such file, try creating a new ID
    while($tries-- && file_exists($this->getImgDir() . $id . $this->getSuffix())) {
        $id = $this->_generateRandomId();
        $this->_setId($id);
    }
    $this->_generateImage($id, $this->getWord());

    if (mt_rand(1, $this->getGcFreq()) == 1) {
        $this->_gc();
    }
    return $id;
}

Zend/Captcha/Image.php

protected function _generateImage($id, $word)
{
    if (!extension_loaded("gd")) {
        //require_once 'Zend/Captcha/Exception.php';
        throw new Zend_Captcha_Exception("Image CAPTCHA requires GD extension");
    }

    if (!function_exists("imagepng")) {
        //require_once 'Zend/Captcha/Exception.php';
        throw new Zend_Captcha_Exception("Image CAPTCHA requires PNG support");
    }

    if (!function_exists("imageftbbox")) {
        //require_once 'Zend/Captcha/Exception.php';
        throw new Zend_Captcha_Exception("Image CAPTCHA requires FT fonts support");
    }

    $font = $this->getFont();

    if (empty($font)) {
        //require_once 'Zend/Captcha/Exception.php';
        throw new Zend_Captcha_Exception("Image CAPTCHA requires font");
    }

    $w     = $this->getWidth();
    $h     = $this->getHeight();
    $fsize = $this->getFontSize();

    $img_file   = $this->getImgDir() . $id . $this->getSuffix();
    if(empty($this->_startImage)) {
        $img        = imagecreatetruecolor($w, $h);
    } else {
        $img = imagecreatefrompng($this->_startImage);
        if(!$img) {
            //require_once 'Zend/Captcha/Exception.php';
            throw new Zend_Captcha_Exception("Can not load start image");
        }
        $w = imagesx($img);
        $h = imagesy($img);
    }
    $text_color = imagecolorallocate($img, 0, 0, 0);
    $bg_color   = imagecolorallocate($img, 255, 255, 255);
    imagefilledrectangle($img, 0, 0, $w-1, $h-1, $bg_color);
    $textbox = imageftbbox($fsize, 0, $font, $word);
    $x = ($w - ($textbox[2] - $textbox[0])) / 2;
    $y = ($h - ($textbox[7] - $textbox[1])) / 2;
    imagefttext($img, $fsize, 0, $x, $y, $text_color, $font, $word);

   // generate noise
    for ($i=0; $i<$this->_dotNoiseLevel; $i++) {
       imagefilledellipse($img, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color);
    }
    for($i=0; $i<$this->_lineNoiseLevel; $i++) {
       imageline($img, mt_rand(0,$w), mt_rand(0,$h), mt_rand(0,$w), mt_rand(0,$h), $text_color);
    }

    // transformed image
    $img2     = imagecreatetruecolor($w, $h);
    $bg_color = imagecolorallocate($img2, 255, 255, 255);
    imagefilledrectangle($img2, 0, 0, $w-1, $h-1, $bg_color);
    // apply wave transforms
    $freq1 = $this->_randomFreq();
    $freq2 = $this->_randomFreq();
    $freq3 = $this->_randomFreq();
    $freq4 = $this->_randomFreq();

    $ph1 = $this->_randomPhase();
    $ph2 = $this->_randomPhase();
    $ph3 = $this->_randomPhase();
    $ph4 = $this->_randomPhase();

    $szx = $this->_randomSize();
    $szy = $this->_randomSize();

    for ($x = 0; $x < $w; $x++) {
        for ($y = 0; $y < $h; $y++) {
            $sx = $x + (sin($x*$freq1 + $ph1) + sin($y*$freq3 + $ph3)) * $szx;
            $sy = $y + (sin($x*$freq2 + $ph2) + sin($y*$freq4 + $ph4)) * $szy;

            if ($sx < 0 || $sy < 0 || $sx >= $w - 1 || $sy >= $h - 1) {
                continue;
            } else {
                $color    = (imagecolorat($img, $sx, $sy) >> 16)         & 0xFF;
                $color_x  = (imagecolorat($img, $sx + 1, $sy) >> 16)     & 0xFF;
                $color_y  = (imagecolorat($img, $sx, $sy + 1) >> 16)     & 0xFF;
                $color_xy = (imagecolorat($img, $sx + 1, $sy + 1) >> 16) & 0xFF;
            }
            if ($color == 255 && $color_x == 255 && $color_y == 255 && $color_xy == 255) {
                // ignore background
                continue;
            } elseif ($color == 0 && $color_x == 0 && $color_y == 0 && $color_xy == 0) {
                // transfer inside of the image as-is
                $newcolor = 0;
            } else {
                // do antialiasing for border items
                $frac_x  = $sx-floor($sx);
                $frac_y  = $sy-floor($sy);
                $frac_x1 = 1-$frac_x;
                $frac_y1 = 1-$frac_y;

                $newcolor = $color    * $frac_x1 * $frac_y1
                          + $color_x  * $frac_x  * $frac_y1
                          + $color_y  * $frac_x1 * $frac_y
                          + $color_xy * $frac_x  * $frac_y;
            }
            imagesetpixel($img2, $x, $y, imagecolorallocate($img2, $newcolor, $newcolor, $newcolor));
        }
    }

    // generate noise
    for ($i=0; $i<$this->_dotNoiseLevel; $i++) {
        imagefilledellipse($img2, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color);
    }
    for ($i=0; $i<$this->_lineNoiseLevel; $i++) {
       imageline($img2, mt_rand(0,$w), mt_rand(0,$h), mt_rand(0,$w), mt_rand(0,$h), $text_color);
    }

    imagepng($img2, $img_file);
    imagedestroy($img);
    imagedestroy($img2);
}

基本步骤

  1. 使用基底图生成缓冲图片
  2. 输出验证码文本
  3. 根据点和线的噪音级别生成噪音图形
  4. 进行波形扭曲变换
  5. 再次生成噪声点和线
  6. 保存图片到PNG格式的文件

** 验证码管理 **

验证码管理的基础工作在 Zend_Captcha_Word中完成,基本实现思路

  • 将生成的验证码code存入session中

    Zend/Captcha/Word.php

    public function generate()
    {
        if (!$this->_keepSession) {
            $this->_session = null;
        }
        $id = $this->_generateRandomId();
        $this->_setId($id);
        $word = $this->_generateWord();
        $this->_setWord($word);
        return $id;
    }
    
    protected function _setWord($word)
    {
        $session       = $this->getSession();
        $session->word = $word;
        $this->_word   = $word;
        return $this;
    }
    
  • 验证时直接从session中读取并验证

    Zend/Captcha.Word.php

    public function isValid($value, $context = null)
    {
        ...
    
        $this->_id = $value['id'];
        if ($input !== $this->getWord()) {
            $this->_error(self::BAD_CAPTCHA);
            return false;
        }
    
        return true;
    }
    
    public function getWord()
    {
        if (empty($this->_word)) {
            $session     = $this->getSession();
            $this->_word = $session->word;
        }
        return $this->_word;
    }
    
  • 清除session中的验证码

    通过控制Session数据失效时间和步数来清除Session中的验证码

    Zend/Captcha.Word.php

    public function getSession()
    {
        if (!isset($this->_session) || (null === $this->_session)) {
            $id = $this->getId();
            if (!class_exists($this->_sessionClass)) {
                //require_once 'Zend/Loader.php';
                Zend_Loader::loadClass($this->_sessionClass);
            }
            $this->_session = new $this->_sessionClass('Zend_Form_Captcha_' . $id);
            $this->_session->setExpirationHops(1, null, true);
            $this->_session->setExpirationSeconds($this->getTimeout());
        }
        return $this->_session;
    }
    

** 清理失效验证码图片 **

遍历验证码存储的文件夹,按创建世界删除有效期(最长有效期默认10分钟)之外的图片文件。

参照

Zend/Captcha/Image.php#_gc

GC在生成验证码的时候按概率随机触发

Zend/Captcha/Image.php#generate

if (mt_rand(1, $this->getGcFreq()) == 1) {
    $this->_gc();
}

除自己生成并管理验证码外,Zend Captcha还支持直接使用reCaptcha服务来管理验证码


配置参数管理 ←o→ 分页组件的实现

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容

  • JCaptcha 简介 CAPTCHA 全称 Completely Automated Public Turing...
    谁在烽烟彼岸阅读 665评论 0 0
  • 背景 验证码就是把一串随机产品的数字动态生成一幅图片,再加上干扰元素。此时用户可以通过肉眼能识别里面的数字或者字符...
    dy2903阅读 1,995评论 0 7
  • 控制器1.文件名不需要加后缀,全部小写2.类名首字母大写,继承CI_Controller基类3.以下划线开头或者非...
    栋栋晓阅读 1,316评论 1 6
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,100评论 18 139
  • Php:脚本语言,网站建设,服务器端运行 PHP定义:一种服务器端的HTML脚本/编程语言,是一种简单的、面向对象...
    廖马儿阅读 2,084评论 2 38