VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • 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>
复制代码

这样就完成了。

 

整理仅供个人学习使用,若以上内容损害了您的合法权益,请联系我予以删除

相关教程