登录页面动态验证码的设置

采用php中创建对象的思想进行动态验证码的设置

1、创建出一个背景图片,用来存放动态码输出位置

function createImage(){
         // 创建图片对象,并设置图片的宽高 imagecreatetruecolor
         $this->image = imagecreatetruecolor($this->width, $this->height);
         // 图片创建背景颜色
         // rand(下界,上界), php中的随机数
         $backgroundColor = imagecolorallocate($this->image, rand(225,255), rand(225, 255), rand(225,255));
         // 填充背景颜色
         imagefill($this->image, 0, 0, $backgroundColor);
         // 给验证码设置边框
         $borderColor = imagecolorallocate($this->image, 0, 0 , 0); // 黑色边框
         // 绘制边框
         imagerectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $borderColor);
       }

2、设置动态码中的干扰信息,可以为(点, 线·······)

function setDisturbElement(){
        // 随机生成干扰项的数量
        $lineNumbers = rand(2, 4);
        // 通过for循环循环生成每个干扰线
        for ($i=0; $i < $lineNumbers; $i++) {
          // 左上角到右下角
          $x1 = rand(2, $this->width / 2);
          $y1 = rand(2, $this->height / 2);
          $x2 = rand($this->width/2, $this->width-2);
          $y2 = rand($this->height/2, $this->height-2);
          $color = imagecolorallocate($this->image, rand(100, 200), rand(100, 200), rand(100, 200));
          // 绘制线条
          imageline($this->image, $x1, $y1, $x2, $y2, $color);
        }
      }

 

3、生成验证码字符串中的字符串信息,并将其存储到本地的session中以便于信息的验证

private function createCheckingCode(){
         $code = "0123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDASZXCVBNM";
         // echo $code;
         // 接受验证码的字符串
         $string = '';
         for ($i=0; $i < $this->codeNumbers; $i++) {
             $index = rand(0, (strlen($code) - 1));
             // echo $index;
             $string .= substr($code, $index, 1); // 获取code字符串的下标为$index的字符,添加到String字符串中
         }
         return $string;
       }
       // 为图像设置验证码文本
       function setValidationCode(){
         for ($i=0; $i < $this->codeNumbers; $i++) {
             $x = rand(1, 4) + $this->width * $i / $this->codeNumbers;
             $y = rand(1, $this->height/4);
             $color= imagecolorallocate($this->image, rand(0,128), rand(0, 128), rand(0, 128));
             $fontSize = rand(6, 8);
             // 绘制文本
             imagestring($this->image,$fontSize, $x, $y, $this->checkingCode[$i], $color);
         }
       }
       // 存储验证码到session中
       function saveVcodeToSession(){
           include_once('session.php');
           $session  = new Session();
           $session->set('checkingCode', $this->checkingCode, 10);
       }

4、对生成的验证码进行前端输出

function outputImage(){
         $this->createImage(); // 绘制图片
         $this->setDisturbElement(); // 绘制线条
         $this->setValidationCode();  // 生成文本
         header("Content-Type:image/png"); // 设置文档输出类型, png类型 | jpg
         imagepng($this->image);
       }

完整代码

登录页面动态验证码的设置登录页面动态验证码的设置

<?php
     // 要将验证码的码值保存在session,方便前端进行比对验证
     session_start();
     /**
      * 验证码生成类
      */
     class Validationcode
     {
       // private ,私有成员,只限于内部使用
       private $width;
       private $height;
       private $codeNumbers; // 验证码的个数
       private $image; // 将来要输出的图像
       private $checkingCode; // 验证码字符串,将被保存在session数组中
       function __construct($width = 80, $height = 30, $codeNumbers = 4) // 如果不提供参数,则80, 30, 4 为默认值
       {
           $this->width        = $width;
           $this->height       = $height;
           $this->codeNumbers  = $codeNumbers;
           $this->checkingCode = $this->createCheckingCode(); // 随机生成验证码
           // echo $this->checkingCode;
       }
       // 创建背景图片函数
       function createImage(){
         // 创建图片对象,并设置图片的宽高 imagecreatetruecolor
         $this->image = imagecreatetruecolor($this->width, $this->height);
         // 图片创建背景颜色
         // rand(下界,上界), php中的随机数
         $backgroundColor = imagecolorallocate($this->image, rand(225,255), rand(225, 255), rand(225,255));
         // 填充背景颜色
         imagefill($this->image, 0, 0, $backgroundColor);
         // 给验证码设置边框
         $borderColor = imagecolorallocate($this->image, 0, 0 , 0); // 黑色边框
         // 绘制边框
         imagerectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $borderColor);
       }
       // 设置干扰项
       function setDisturbElement(){
         // 随机生成干扰项的数量
         $lineNumbers = rand(2, 4);
         // 通过for循环循环生成每个干扰线
         for ($i=0; $i < $lineNumbers; $i++) {
           // 左上角到右下角
           $x1 = rand(2, $this->width / 2);
           $y1 = rand(2, $this->height / 2);
           $x2 = rand($this->width/2, $this->width-2);
           $y2 = rand($this->height/2, $this->height-2);
           $color = imagecolorallocate($this->image, rand(100, 200), rand(100, 200), rand(100, 200));
           // 绘制线条
           imageline($this->image, $x1, $y1, $x2, $y2, $color);
         }
       }
       // 对创建的验证码图片进行输出
       function outputImage(){
         $this->createImage(); // 绘制图片
         $this->setDisturbElement(); // 绘制线条
         $this->setValidationCode();  // 生成文本
         header("Content-Type:image/png"); // 设置文档输出类型, png类型 | jpg
         imagepng($this->image);
       }
       // 生成验证码字符串
       // 私有的函数
       private function createCheckingCode(){
         $code = "0123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDASZXCVBNM";
         // echo $code;
         // 接受验证码的字符串
         $string = '';
         for ($i=0; $i < $this->codeNumbers; $i++) {
             $index = rand(0, (strlen($code) - 1));
             // echo $index;
             $string .= substr($code, $index, 1); // 获取code字符串的下标为$index的字符,添加到String字符串中
         }
         return $string;
       }
       // 为图像设置验证码文本
       function setValidationCode(){
         for ($i=0; $i < $this->codeNumbers; $i++) {
             $x = rand(1, 4) + $this->width * $i / $this->codeNumbers;
             $y = rand(1, $this->height/4);
             $color= imagecolorallocate($this->image, rand(0,128), rand(0, 128), rand(0, 128));
             $fontSize = rand(6, 8);
             // 绘制文本
             imagestring($this->image,$fontSize, $x, $y, $this->checkingCode[$i], $color);
         }
       }
       // 存储验证码到session中
       function saveVcodeToSession(){
           include_once('session.php');
           $session  = new Session();
           $session->set('checkingCode', $this->checkingCode, 10);
       }
     }
     $vcode = new Validationcode();
     $vcode->outputImage();
     $vcode->saveVcodeToSession();
  ?>
php