• 2019-07-23 用类写一个简单验证码


    1.验证码代码如下,新建一个含如下代码的PHP文件,此处取名为ValidateCode.php:

    <?php
    /*
     * ValidateCode.php
     */
    class ValidateCode {
        private $charset = '0123456789';
        private $code;
        private $codelen = 4;
        private $width = 163;
        private $height = 30;
        private $img;
        private $font;
        private $fontsize = 20;
        private $fontcolor;
    
        public function __construct($size) {
            $this->font = dirname(__file__) . '/t1.ttf';
            $this->codelen = $size;
            $this->charset = str_repeat($this->charset, 4);
        }
    
        private function createCode() {
            $_len = strlen($this->charset)-1;
            for ($i=0;$i<$this->codelen;$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(157,255), mt_rand(157,255), mt_rand(157,255));
            imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color);
        }
    
        private function createFont() {
            $_x = ($this->width - 10) / $this->codelen;
            for ($i=0;$i<$this->codelen;$i++) {
                $this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
                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);
        }
    }
    ?>

    2.新建一个生成验证码的页面,此处取名为Code.php,然后引入上面的php代码,实例化类,调用上面的doimg方法。

    <?php
    
    include("ValidateCode.php");
    $obj = new ValidateCode(4);
    $obj->doimg(); 
    
    ?>

    3.上面的步骤做完后,就可以在其它页面通过img标签的src属性调用Code.php。

    <html>
        <meta charset=utf8>
        <head><title>验证码</title></head>
        <body>
            <form action="">
            用户名:<input type="text" name="username"><br>
            用户密码:<input type="password" name="userpwd"><br>
            验证码:<input type="text" name="code"><img src="code.php" alt="yzm" onclick="this.src='code.php?id='+Math.random()" width="90px">
            </form>
        </body>
    </html>

    这样就完成了。

  • 相关阅读:
    [HTML]安卓下<video>无法点击播放
    [JSP]解决Maven创建项目失败
    [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis)
    [转]Express入门教程:一个简单的博客
    [转][译]关于CSS中的float和position和z-index
    [CSS]多浏览器兼容的垂直居中,兼容多个IE
    [转]非常实用的15款开源PHP类库
    [PHP]使用PHPMailer发送带附件并支持HTML内容的邮件
    ROS中阶笔记(一):机器人系统设计—ROS系统下连接外部传感器
    ROS入门笔记(十三):分布式通信
  • 原文地址:https://www.cnblogs.com/zhangxu-fasu/p/11232856.html
Copyright © 2020-2023  润新知