chenguosong

验证码实例如下看不懂的先看gd库的那一篇,session不懂的看from提交的数据都哪里了那一篇,欢迎关注

index.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<form name="form" method="post" action="">
    验证码(大写):<br>
    <img src="hcon.php" width="70" height="18" border="0" align="bottom">
    <input type="text" name="yanzhengma">
    <br>
    <input type="Submit" name="Submit" value="登录" >
</form> 
</body>
</html>
<?php
session_start();
if(isset($_POST["Submit"]) && $_POST["Submit"]!=""){
    $yanzhengma=$_POST["yanzhengma"];

    if($yanzhengma=="")
    { echo "<script> alert(\'验证码为空\');window.location.href=\'index.php\';</script>";}
    else if($yanzhengma!=$_SESSION["jiancha"]){
        echo "<script> alert(\'输错了\');window.location.href=\'index.php\';</script>";
    }
    else{
        echo "<script> alert(\'登录成功\');window.location.href=\'index.php\';</script>";
    }

}

?>

yanzhengma.php

<?php
class yanzhengma2{
    protected $width;
    protected $height;
    protected $im;
    protected $len;
    protected $code;

    public function __construct($width=100,$height=30,$len=5){
        $this->width=$width;
        $this->height=$height;
        $this->len=$len;
    }

    public function render(){
        $im= imagecreatetruecolor($this->width,$this->height);
        $color=imagecolorallocate($im, 200,200,200); 
        imageFill($this->im=$im,0,0,$color);//画布背景填充颜色,默认黑色
        $this->text();
        $this->line();
        $this->pix();
        $this->show();
        return $this->code;
 
    }

    //显示渲染
    protected function show(){
        header(\'Content-type:image/png\');
        imagepng($this->im);
    }

    //绘制验证码
    protected function text(){
        $font=$font="c:/windows/fonts/simhei.ttf";
        $text=\'abcdefghigklmnopqrstuvwxyz0123456789\';
        for($i=0;$i<$this->len;$i++){
            $x=$this->width/$this->len;
            $angle=mt_rand(-20,20);
            $this->code.=$textn=strtoupper($text[mt_rand(0,strlen($text)-1)]);
            $box=imagettfbbox(20,$angle,$font,$textn);           

            imagettftext(
                $this->im,
                20,$angle,
                $x*$i+10,
                $this->height/2-($box[7]-$box[1])/2,
                $this->textcolor(),
                $font,
                $textn
            );
        }
    }
    //绘制干扰点
    protected function pix(){
        for($i=0;$i<300;$i++){
                imagesetpixel($this->im,mt_rand(0,$this->width),mt_rand(0,$this->height),$this->color());
        }

    }
    //绘制干扰线
    protected function line(){
        
        for($i=0;$i<6;$i++){
            imagesetthickness($this->im,mt_rand(1,2));
            imageline($this->im,
            mt_rand(0,$this->width),
            mt_rand(0,$this->height),
            mt_rand(0,$this->width),
            mt_rand(0,$this->height),
            $this->color()
        );
        }
    }
    //生成随机颜色
    protected function color(){
        return imagecolorallocate($this->im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    }

    //生成随机文字颜色(深)
    protected function textcolor(){
            return imagecolorallocate($this->im,mt_rand(0,50),mt_rand(0,50),mt_rand(0,50));
        }
}

?>

hcon.php

<?php
session_start();
include \'yanzhengma.php\';
$yanzhengma=new yanzhengma2(200,40);
$code=$yanzhengma->render();
$_SESSION["jiancha"]=$code;  
?>

分类:

技术点:

相关文章: