Home Backend Development PHP Tutorial php 验证码程序_PHP

php 验证码程序_PHP

Jun 01, 2016 pm 12:30 PM
i image var program verify

验证码

        // -------------------------------------------------------------
        // 名称: my_authimg class
        //
        // 用途: 根据图片数字字母验证
        // 实例:
        // $ai = new my_authimg();
        // $ai->render();
        //
        // 判断结果是否正确
        // $ai = new my_authimg();
        // $ai->is_true($str);
        //
        // @作者: hightman
        // @版本: 0.0.0
        // @时间: 2005/05/26
        // $Id: $
        // -------------------------------------------------------------
       
        class my_authimg {
                // public var
                var $width          = 72;
                var $height         = 20;
                var $text_font      = 5;
                var $text_space     = 10;
                var $text_length    = 5;
                var $sess_name      = "__auth_xyz_123__";
                var $num_only       = false;
               
                // private
                var $_top           = 1;
                var $_move          = 3;
                var $_lines         = 5;
               
                function auth_image($params = array()) {
                        settype($params, "array");
                        foreach ($params as $key => $value) {
                                if (isset($this->$key)) {
                                        $this->$key = $value;
                                }
                        }
                        $this->_lines = intval($this->height / 10);
                }
               
                function set_sess_name($name = "") {
                        if (!empty($name)) {
                                $this->sess_name = $name;
                        }
                }
               
                function set_text_length($length = 5) {
                        $this->text_length = $length;
                }
               
                function set_img_size($width = 100, $height = 15) {
                        $this->width = $width;
                        $this->height = $height;
                }
               
                function get_sess_value() {
                        @session_start();
                       
                        $sess_key = $this->sess_name;
                        $sess_value = NULL;               

                        if (isset($_SESSION) && isset($_SESSION[$sess_key])) {
                                $sess_value = $_SESSION[$sess_key];
                                unset($_SESSION[$sess_key]);                               
                        } else {
                                global $HTTP_SESSION_VARS;
                               
                                if (isset($HTTP_SESSION_VARS[$sess_key])) {
                                        $sess_value = $HTTP_SESSION_VARS[$sess_key];
                                        session_unregister($this->sess_name);
                                }
                        }
                       
                        if (isset($GLOBALS[$sess_key])) {
                                unset($GLOBALS[$sess_key]);
                        }

                        return $sess_value;
                }
               
                function is_true($str) {
                        $sess_value = $this->get_sess_value();
                        return (!strcasecmp($sess_value, $str));
                }
               
                function render() {
                        $radix = "123456789";
                        if (!$this->num_only) {
                                $radix .= "ABCDEFGHIJKLMNPQRSTUVWXYZ";
                        }
                        $radix_len = strlen($radix);
      
                        // 种下随机种子
                        mt_srand();

                        // 初始化图片
                        $image          = ImageCreate($this->width, $this->height);
                       
                        // 设定颜色
                        $r = mt_rand() % 255; //2 ? 255 : 0;
                        $g = mt_rand() % 255; //2 ? 255 : 0;
                        $b = mt_rand() % 255; //2 ? 255 : 0;
                        $fgcolor        = ImageColorAllocate($image, $r, $g, $b);
                        $bgcolor        = ImageColorAllocate($image, 255 - $r, 255 - $g, 255 - $b);
                        $silver                = ImageColorAllocate($image, (255 - $r) * 2, (255 - $g) * 2, (255 - $b) * 2);
                       
                        // 生成背景
                        ImageFill($image, 0, 0, $bgcolor);                       
                       
                        // 画出横向干扰线
                        $line_space = ceil($this->height / ($this->_lines + 1));
                        $line_move  = ceil($line_space * 2);
                        for ($i = 1; $i _lines; $i++) {
                                $y  = $line_space * $i;
                                $y2 = $y + (($i - rand(0, 2 * $i)) % 2) * $line_move;
                                ImageLine($image, 0, $y, $this->width, $y2, $silver);
                        }
                       
                        // 画出干扰点
                        $pixel_num = intval($this->height * $this->width / 20);
                        for($i = 0; $i                                 $x = mt_rand() % $this->width;
                                $y = mt_rand() % $this->height;
                                ImageSetPixel($image, $x, $y, $silver);
                        }
                       
                        // 画出字符
                        $rand_str = "";
                        $step_len = intval($this->width / ($this->text_length + 1));
                        $left_len = $step_len - 6;

                        for ($i = 0; $i text_length; $i++) {
                                $x = $left_len + ($step_len * $i);
                                $y = ($i % 2) * $this->_move + $this->_top;

                                $rand = mt_rand(0, $radix_len - 1);
                                $rand_str .= substr($radix, $rand, 1);
                               
                                ImageString($image, $this->text_font, $x, $y, substr($radix, $rand, 1), $fgcolor);
                        }
                       
                        // 设定 SESSION 值
                        @session_start();
                        if (isset($_SESSION)) {
                                $sess_key = $this->sess_name;
                                $_SESSION[$sess_key] = $rand_str;
                        }
                        else {
                                session_register($this->sess_name, $rand_str);
                        }
                       
                        // 输出图象
                        header("Content-type: image/png");
                        ImagePNG($image);
                        ImageDestroy($image);
                }
        }
?>

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to make Google Maps the default map in iPhone How to make Google Maps the default map in iPhone Apr 17, 2024 pm 07:34 PM

How to make Google Maps the default map in iPhone

How to verify signature in PDF How to verify signature in PDF Feb 18, 2024 pm 05:33 PM

How to verify signature in PDF

What is the best graphics card for i7 3770? What is the best graphics card for i7 3770? Dec 29, 2023 am 09:12 AM

What is the best graphics card for i7 3770?

Detailed method to unblock using WeChat friend-assisted verification Detailed method to unblock using WeChat friend-assisted verification Mar 25, 2024 pm 01:26 PM

Detailed method to unblock using WeChat friend-assisted verification

How to write a simple countdown program in C++? How to write a simple countdown program in C++? Nov 03, 2023 pm 01:39 PM

How to write a simple countdown program in C++?

Clock app missing in iPhone: How to fix it Clock app missing in iPhone: How to fix it May 03, 2024 pm 09:19 PM

Clock app missing in iPhone: How to fix it

How to open a website using Task Scheduler How to open a website using Task Scheduler Oct 02, 2023 pm 11:13 PM

How to open a website using Task Scheduler

iOS 17: How to organize iMessage apps in Messages iOS 17: How to organize iMessage apps in Messages Sep 18, 2023 pm 05:25 PM

iOS 17: How to organize iMessage apps in Messages

See all articles