欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-03 12:19:18
1.验证码代码如下,新建一个含如下代码的PHP文件,此处取名为ValidateCode.php: 2.新建一个生成验证码的页面,此处取名为Code.php,然后引入上面的php代码,实例化类,调用上面的doimg方法。 3.上面的步骤做完后,就可以在其它页面通过img标签的src属性调用Code.p ......

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>

这样就完成了。