zoukankan      html  css  js  c++  java
  • 验证码

    captcha.php

      1 <?php
      2 
      3 /**
      4  * @PHP验证码类
      5  * 使用方法:
      6  * $image=new Captcha();
      7  * $image->config('宽度','高度','字符个数','验证码session索引');
      8  * $image->create();//这样就会向浏览器输出一张图片
      9  * //所有参数都可以省略,
     10  * 默认是:宽80 高20 字符数4 验证码session索引captcha_code
     11  * 第四个参数即把验证码存到$_SESSION['captcha_code']
     12  * 最简单使用示例:
     13  * $image=new Captcha();
     14  * $image->create();//这样就会向浏览器输出一张图片
     15  */
     16 class Captcha
     17 {
     18   private $width=80,$height=20,$codenum=4;
     19   public $checkcode;   //产生的验证码
     20   private $checkimage;  //验证码图片
     21   private $disturbColor = ''; //干扰像素
     22   private $session_flag='captcha_code';//存到session中的索引
     23 //尝试开始session
     24   function __construct(){
     25     @session_start();
     26   }
     27   /*
     28   * 参数:(宽度,高度,字符个数)
     29   */
     30   function config($width='80',$height='20',$codenum='4',$session_flag='captcha_code')
     31   {
     32     $this->width=$width;
     33     $this->height=$height;
     34     $this->codenum=$codenum;
     35     $this->session_flag=$session_flag;
     36   }
     37   function create()
     38   {
     39     //输出头
     40     $this->outFileHeader();
     41     //产生验证码
     42     $this->createCode();
     43     //产生图片
     44     $this->createImage();
     45     //设置干扰像素
     46     $this->setDisturbColor();
     47     //往图片上写验证码
     48     $this->writeCheckCodeToImage();
     49     imagepng($this->checkimage);
     50     imagedestroy($this->checkimage);
     51     $_SESSION[$this->session_flag]=$this->checkcode;
     52   }
     53   /*
     54     * @brief 输出头
     55     */
     56   private function outFileHeader()
     57   {
     58     header ("Content-type: image/png");
     59   }
     60   /**
     61    * 产生验证码
     62    */
     63   private function createCode()
     64   {
     65     $this->checkcode = strtoupper(substr(md5(rand()),0,$this->codenum));
     66   }
     67   /**
     68    * 产生验证码图片
     69    */
     70   private function createImage()
     71   {
     72     $this->checkimage = @imagecreate($this->width,$this->height);
     73     $back = imagecolorallocate($this->checkimage,255,255,255);
     74     $border = imagecolorallocate($this->checkimage,0,0,0);
     75     imagefilledrectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$back); // 白色底
     76     imagerectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$border);  // 黑色边框
     77   }
     78   /**
     79    * 设置图片的干扰像素
     80    */
     81   private function setDisturbColor()
     82   {
     83     for ($i=0;$i<=200;$i++)
     84     {
     85       $this->disturbColor = imagecolorallocate($this->checkimage, rand(0,255), rand(0,255), rand(0,255));
     86       imagesetpixel($this->checkimage,rand(2,128),rand(2,38),$this->disturbColor);
     87     }
     88   }
     89   /**
     90    * 在验证码图片上逐个画上验证码
     91    */
     92   private function writeCheckCodeToImage()
     93   {
     94     for ($i=0;$i<$this->codenum;$i++)
     95     {
     96       $bg_color = imagecolorallocate ($this->checkimage, rand(0,255), rand(0,128), rand(0,255));
     97       $x = floor($this->width/$this->codenum)*$i;
     98       $y = rand(0,$this->height-15);
     99       imagechar ($this->checkimage, rand(5,8), $x+5, $y, $this->checkcode[$i], $bg_color);
    100     }
    101   }
    102   function __destruct()
    103   {
    104     unset($this->width,$this->height,$this->codenum,$this->session_flag);
    105   }
    106 }
    107 ?>

    引入类 yanzhengma.php

    <?php 
    require_once('captcha.php');
    $image =  new Captcha();
    $image ->create();
     1 <?php 
     2 ob_start();
     3 session_start();
     4 if(!empty($_POST)){
     5     $answer = $_POST['answer'];
     6     $captcha = $_SESSION['captcha_code'];
     7     if($answer !== $captcha){
     8         echo "<script>alert('验证码错误');history.go(-1);</script>";
     9         die;
    10     }
    11 
    12 
    13 
    14 ?>
    15 
    16 
    17 <form name="login1" method="post" action=" " onsubmit="return checklogin1();">            
    18             <ul class="loginRegist" style="margin-top: 85px;">
    19                 <li>
    20                     <span class="titleOne" style=""> 
    21                         <label for="username">账户</label>
    22                     </span>
    23                     <span style=" 200px;position: relative;" class="titleInput">
    24                         <input id="username" name="username" class="regUserName" type="text" value="" placeholder="" />
    25                     </span>
    26                 
    27                 </li>               
    28                 <li>
    29                     <span class="titleOne" style="">
    30                         <label for="password">密码</label>
    31                     </span>
    32                     <span class="titleInput" style="position: relative;">
    33                         <input id="password" name="password"  type="password" placeholder="" maxlength="32" /> 
    34                     </span>
    35                     
    36                 </li>
    37                
    38                 <li>
    39                     <span class="titleOne"><label for="username">问题验证</label></span>
    40                     <span style=" 370px;display: inline-block;text-align: left;">
    41                         <span class="titleInput"><input name="answer" id="answer"  class="codeInput" type="text"/></span>
    42                         <!-- <a class="msgCodeBindBtnA" style="color: rgb(153, 153, 153);font-size: 13px;">23+51=?</a> -->
    43                         <img src="diaocaptcha.php" alt="验证码" onclick="this.src='diaocaptcha.php?='+Math.random()" />
    44                     </span>
    45                 </li>               
    46             </ul>
    47             <div class="registCue">
    48                  <input class="regeditButton disagree" type="submit" value="登   陆">
    49             </div>
    50 </form>
  • 相关阅读:
    Tomcat原理与实践
    Spring Boot入门与实践
    Docker安装及使用
    JDK源码解析——集合(一)数组 ArrayList
    浅谈mysql底层索引
    微信小程序全局配置知识点
    uniapp全屏高度
    npm node-sass报错
    微信小程序接口配置问题
    微信小程序的设计流程
  • 原文地址:https://www.cnblogs.com/php-qiuwei/p/8695129.html
Copyright © 2011-2022 走看看