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

2019-07-24 09:19:14来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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>

这样就完成了。

 


原文链接:https://www.cnblogs.com/zhangxu-fasu/p/11232856.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:2019-07-23 类的封装性

下一篇:2019-07-23 static 和 const 关键字的应用