php验证码

1.文件结构
  CreateCaptcha.php  //生成验证码类
  index.html       //验证码前端页面
  create.php      //生成验证码  
  verify.php        //验证程序

2.CreateCaptcha.php

<?php
/**
* 字母+数字的验证码生成
*/

class CreateCaptcha
{
    private $imgWidth;//验证码宽度
    private $imgHeight;//验证码高度
    private $length;//验证码字符个数
    private $fontSize;//验证码字符大小,1,2,3,4,5
    private $backDrop;//是否加干扰元素

    public function __construct($imgWid=0,$imgHei=0,$len=0,$fontSize=0,$backDrop=true)
    {
        (empty($imgWid))?$this->imgWidth=100:$this->imgWidth=$imgWid;
        (empty($imgHei))?$this->imgHeight=30:$this->imgHeight=$imgHei;
        (empty($len))?$this->length=4:$this->length=$len;
        (empty($fontSize))?$this->fontSize=10:$this->fontSize=$fontSize;
        (empty($backdrop))?$this->backDrop=$backDrop:$this->backDrop=true;
    }
    /**
    *@param int $imgWid;
    *@param int $imgHei;
    *@param int $len;
    *@param int $fontSize;
    *@param bool $backDrop;
    *@return png;
    */
    public function sendCaptcha()
    {
        // echo $this->backDrop;exit;
        // 开启session
        session_start();
        //1.创建黑色画布
        $image = imagecreatetruecolor($this->imgWidth, $this->imgHeight);
         
        //2.为画布定义(背景)颜色
        $bgcolor = imagecolorallocate($image, 255, 255, 255);
         
        //3.填充颜色
        imagefill($image, 0, 0, $bgcolor);
         
        // 4.设置验证码内容
         
        //4.1 定义验证码的内容
        $content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
         
        //4.1 创建一个变量存储产生的验证码数据,便于用户提交核对
        $captcha = "";
        for ($i = 0; $i < $this->length; $i++) {
            // 字体大小
            $fontsize = $this->fontSize;
            // 字体颜色
            $fontcolor = imagecolorallocate($image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200));
            // 设置字体内容
            $fontcontent = substr($content, mt_rand(0, strlen($content)), 1);
            $captcha .= $fontcontent;
            // 显示的坐标,居中
            $x = ($i * $this->imgWidth / $this->length) + mt_rand(5, 8);
            $y = ($this->imgHeight-$this->fontSize*2)/2;
            // 填充内容到画布中
            imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
        }
        $_SESSION["captcha"] = $captcha;
        //是否加干扰
         if ($this->backDrop) {
            //4.3 设置背景干扰元素
            for ($$i = 0; $i < 200; $i++) {
                $pointcolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
                imagesetpixel($image, mt_rand(1, $this->imgWidth-1), mt_rand(1, $this->imgHeight-1), $pointcolor);
            }
             
            //4.4 设置干扰线
            for ($i = 0; $i < $this->length; $i++) {
                $linecolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
                imageline($image, mt_rand(1, $this->imgWidth-1), mt_rand(1, $this->imgHeight-1), mt_rand(1, $this->imgWidth-1), mt_rand(1, $this->imgHeight-1), $linecolor);
            }
         }
         
        //5.向浏览器输出图片头信息
        header('content-type:image/png');
         
        //6.输出图片到浏览器
        imagepng($image);
         
        //7.销毁图片
        imagedestroy($image);
    }

}
?>

3.index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>验证码前端页面</title>
</head>
<body>
    <form action="verify.php" method="post"><!-- 跳到验证程序 -->
        <img src="create.php"  onclick="this.src='create.php?'+new Date().getTime();"><br/><!-- 生成验证码,点击更新 -->
        <input type="text" name="captcha" placeholder="请输入图片中的验证码"><br/><!-- 输入验证码 -->
        <input type="submit" value="验证"><!-- 提交并验证 -->
    </form>
</body>
</html>

4.create.php

<?php 
    //引入生成验证码类
    include('CreateCaptcha.php');
    $captcha = new CreateCaptcha();
    $captcha->sendCaptcha();
 ?>

5.verify.php

<?php
/**
 * 接受用户登陆时提交的验证码
 */
session_start();
//1. 获取到用户提交的验证码
$captcha = $_POST["captcha"];
//2. 将session中的验证码和用户提交的验证码进行核对,当成功时提示验证码正确,并销毁之前的session值,不成功则重新提交
if(strtolower($_SESSION["captcha"]) == strtolower($captcha)){
    echo "验证码正确!";
    $_SESSION["captcha"] = "";
}else{
    echo "验证码提交不正确!";
}

?>

  

  

猜你喜欢

转载自www.cnblogs.com/konglingxi/p/9033301.html