php - 一个简单验证码类
admin
2023-07-02 11:23:53
0

The Captcha Class

outputImage();
        }
        
        function check($arr) {
            if (isset($arr) && is_array($arr)) {
                if (isset($arr[$this->sessionvar])) {
                    if ($arr[$this->sessionvar] == $_SESSION[$this->sessionvar]) {
                        return true;
                    }
                }
            }
        }
        
        function randomString($length = 6) {
            $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
            for ($i = 0; $i < $length; $i++) {
                $string .= $chars[mt_rand(0, strlen($chars)-1)];
            }
            return $string;
        }
        
        function outputImage() {
            $this->img = ImageCreate ($this->width, $this->height);
            if ($this->img) {
                header("Content-type: p_w_picpath/png"); 
                
                $bg = ImageColorAllocate($this->img, 255, 255, 255); 
                $txt = ImageColorAllocate($this->img, 0, 0, 0); 
                
                $string = $this->randomString($this->chars);
                
                ImageString($this->img, 31, $this->fontsize, 0, $string, $txt);
                Imagepng($this->img); 
                
                $_SESSION[$this->sessionvar] = $string; 
            }
        }
    }
?>


The class has two main functions, run() and check() – The run function will output an p_w_picpath so make sure the HTML p_w_picpath tag is linked to the file where you use the run() function. Examples below.


Showing the Image

include('captcha.php');
$captcha = new Captcha;
$captcha->run();

If you name that file cp_w_picpath.php you can embed it into forms in the following way:

 

Checking the code

Wherever your form is submitting to is where you should be checking the code. This can either be in the same file or an external file. The following code will allow you to check the $_POST array and make sure the user has entered the correct code.

include('captcha.php');
$captcha = new Captcha;

if ($captcha->check($_POST)) {
    echo 'Correct Code';
} else {
    echo 'Invalid Code';
}


生成图

php - 一个简单验证码类

相关内容

热门资讯

日本首次通过北约清单军援乌克兰 据凤凰卫视报道,日本外务省5月29日宣布,首次通过北约“乌克兰优先需求清单”框架,向乌克兰提供约22...
黑龙江大学科研成果首次被国际数... 近日,黑龙江大学计算机与大数据学院(网络安全学院)朱敬华教授团队在数据挖掘与知识发现领域取得重要科研...
原创 深... 提到深圳的科技创新,你脑海里是不是立刻蹦出南山区的粤海街道? 但可能从今年开始,这个答案要改写了。 ...
一天吃透一个行业09:液冷概念... 来源:市场资讯 (来源:券研社) 一、为什么液冷已成AI算力"刚需"? 芯片功耗突破风冷极限。英伟达...
2026年6月安卓摄影新选择:... 2026年6月安卓摄影新选择:CCD质感手机推荐 2026年6月安卓摄影新选择:CCD质感手机推荐 ...
总书记的人民情怀 | “要坚持... 本报记者 吴 丹原标题:“要坚持健康第一的教育理念”(总书记的人民情怀)
菲律宾防长“装可怜”:中美实力... 【文/观察者网 齐倩】菲律宾政府近年来仗着美国撑腰,在南海议题上“兴风作浪”。中美紧张关系缓和后,菲...
老板燃气灶一直报警是怎么回事 老板燃气灶一直报警是怎么回事原因可能有以下几点:1.熄火保护电路故障造成误报警,导致其长鸣声不断;2...
老板燃气灶一直报警怎么回事 原因可能有以下几点:1.熄火保护电路故障造成误报警,导致其长鸣声不断;2.燃气意外熄灭导致熄火保护功...
燃气灶报警器一直闪灯 这种情况原因有很多种,1、可能是某个零件出现了问题。2、当工业环境中燃气气体泄露,燃气报警器检测到气...