Skip to content

验证码

发布于: | 没有评论 | 分类:

<?php
add_action("wpcf7_init", function () {
    if (function_exists('wpcf7_add_form_tag')) {
        wpcf7_add_form_tag("validate-code", "validate_code_handler", true);
        wpcf7_add_form_tag("validate-code*", "validate_code_handler", true);
    }
});

function validate_code_handler($tag)
{
    $tag = new WPCF7_FormTag($tag);
    if (empty($tag->name)) return '';
    $atts = array();
    if ($tag->is_required()) {
        $atts['aria-required'] = 'true';
    }
    $validation_error = wpcf7_get_validation_error($tag->name);
    $atts['aria-invalid'] = $validation_error ? 'true' : 'false';
    $value = (string) reset($tag->values);
    if (
        $tag->has_option('placeholder')
        or $tag->has_option('watermark')
    ) {
        $atts['placeholder'] = $value;
        $value = '';
    }
    $atts['name'] = $tag->name;
    $atts['type'] = "text";
    $atts['bind'] = $tag->get_option('bind', 'class', true);
    $atts = wpcf7_format_atts($atts);
    $html = sprintf(
        '<span class="wpcf7-form-control-wrap validate-code %1$s"><input autocomplete="off" id="validate-code_input" %2$s />%3$s</span><div id="validate-code_image"><img src="/?get_validate_code=get_validate_code" /></div>',
        sanitize_html_class($tag->name),
        $atts,
        $validation_error
    );
    $html .= '
	<script>
		jQuery(function($){
			$("#validate-code_input").focus(function(){
				$("#validate-code_image img").click()
			})
			$("#validate-code_image img").click(function(){
				$(this).attr("src","/?get_validate_code=get_validate_code&time=' . time() . '")
			})
		});
	</script>
	';
    return $html;
}
add_filter('wpcf7_validate_validate-code', "validate_code_validation_filter", 10, 2);
add_filter('wpcf7_validate_validate-code*', "validate_code_validation_filter", 10, 2);
function validate_code_validation_filter($result, $tag)
{
    $name = $tag->name;
    $value = isset($_POST[$name])
        ? trim(wp_unslash(strtr((string) $_POST[$name], "\n", " ")))
        : '';
    if ($value === '') {
        $result->invalidate($tag, "请输入验证码!");
    } else {
        if (strcmp($value, $_SESSION['custom_validate-code']) <> 0) {
            $result->invalidate($tag, '验证码错误');
        }
    }

    return $result;
}



add_action('init', function () {
    session_start();
    if ($_GET['get_validate_code'] == 'get_validate_code') {
        $ValidateCode = new ValidateCode();
        $ValidateCode->doImg();
        $_SESSION['custom_validate-code'] = $ValidateCode->getCode();
        die;
    } else if ($_GET['eeee'] == 'eeee') {
        echo $_SESSION['custom_validate-code'];
        die;
    }
});

//验证码类
class ValidateCode
{
    private $charset = '*+?abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';    //随机因子
    private $code;                //验证码
    private $code_len = 6;        //验证码长度
    private $width = 200;        //宽度
    private $height = 50;        //高度
    private $img;                //图形资源句柄
    private $font;                //指定的字体
    private $fontsize = 20;        //指定字体大小
    private $fontcolor;            //指定字体颜色


    //构造方法初始化
    public function __construct()
    {
        $this->font = WP_CONTENT_DIR . '/uploads/Fonts/elephant.ttf';
    }


    //生成随机码
    private function createCode()
    {
        $_len = strlen($this->charset) - 1;
        for ($i = 0; $i < $this->code_len; $i++) {
            $this->code .= $this->charset[mt_rand(0, $_len)];
        }
    }


    //生成背景
    private function createBg()
    {
        $this->img = imagecreatetruecolor($this->width, $this->height);
        $color = imagecolorallocate($this->img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
        imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color);
    }


    //生成文字
    private function createFont()
    {
        $_x = $this->width / $this->code_len;
        for ($i = 0; $i < $this->code_len; $i++) {
            $this->fontcolor = imagecolorallocate($this->img, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
            imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(1, 5), $this->height / 1.4, $this->fontcolor, $this->font, $this->code[$i]);
        }
    }


    //生成线条、雪花
    private function createLine()
    {
        for ($i = 0; $i < 6; $i++) {
            $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
            imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
        }
        for ($i = 0; $i < 100; $i++) {
            $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
            imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);
        }
    }


    //输出
    private function outPut()
    {
        header('Content-type:image/png');
        imagepng($this->img);
        imagedestroy($this->img);
    }


    //对外生成
    public function doImg()
    {
        $this->createBg();
        $this->createCode();
        $this->createLine();
        $this->createFont();
        $this->outPut();
    }


    //获取验证码
    public function getCode()
    {
        // return strtolower($this->code);
        return $this->code;
    }
}

标签:

订阅评论
提醒
guest
0 评论
内联反馈
查看所有评论

0
希望看到您的想法,请您发表评论x