A classic PHP verification code class sharing

  • 2021-08-03 09:40:59
  • OfStack

We design a verification code class Vcode through the image processing content of GD library of PHP. Declare this class in the file vcode. class. php, and encapsulate some implementation details in this class through object-oriented features. As long as you provide three parameters for the constructor when creating the object, including the width and height of the created verification code picture and the number of verification code letters, you can successfully create an object of a verification code class. The declaration code for this class is as follows:


<?php
 class Vcode {
  private $width; // Width 
  private $height; // Gao 
  private $num;  // Quantity 
  private $code; // Verification code 
  private $img;  // Image resources 
 
  // Construction method,  3 Parameters 
  function __construct($width=80, $height=20, $num=4) {
   $this->width = $width;
   $this->height = $height;
   $this->num = $num;
   $this->code = $this->createcode(); // Call your own method 
  }
 
  // Gets the verification code of the character,   Used to save in the server 
  function getcode() {
   return $this->code;
  }
 
  // Output image 
  function outimg() {
   // Create background  ( Color,   Size,   Border )
   $this->createback();
 
   // Draw a character  ( Size,   Font color )
   $this->outstring();
 
   // Interfering element ( Point,   Line )
 
   $this->setdisturbcolor();
   // Output image 
   $this->printimg();
  }
 
  // Create background 
  private function createback() {
   // Create a resource 
   $this->img = imagecreatetruecolor($this->width, $this->height);
   // Set random background color 
   $bgcolor = imagecolorallocate($this->img, rand(225, 255), rand(225, 255), rand(225, 255)); 
   // Setting Background Fill 
   imagefill($this->img, 0, 0, $bgcolor);
   // Draw a border 
   $bordercolor = imagecolorallocate($this->img, 0, 0, 0);
 
    imagerectangle($this->img, 0, 0, $this->width-1, $this->height-1, $bordercolor);
  }
 
  // Draw a character 
  private function outstring() {
   for($i=0; $i<$this->num; $i++) {
 
    $color= imagecolorallocate($this->img, rand(0, 128), rand(0, 128), rand(0, 128)); 
 
    $fontsize=rand(3,5); // Font size 
 
    $x = 3+($this->width/$this->num)*$i; // Horizontal position 
    $y = rand(0, imagefontheight($fontsize)-3);
 
    // Draw each character 
    imagechar($this->img, $fontsize, $x, $y, $this->code{$i}, $color);
   }
  }
 
  // Setting interference elements 
  private function setdisturbcolor() {
   // Add points 
   for($i=0; $i<100; $i++) {
    $color= imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255)); 
    imagesetpixel($this->img, rand(1, $this->width-2), rand(1, $this->height-2), $color);
   }
 
   // Add lines 
   for($i=0; $i<10; $i++) {
    $color= imagecolorallocate($this->img, rand(0, 255), rand(0, 128), rand(0, 255)); 
    imagearc($this->img,rand(-10, $this->width+10), rand(-10, $this->height+10), rand(30, 300), rand(30, 300), 55,44, $color);
   }
  }
 
  // Output image 
  private function printimg() {
   if (imagetypes() & IMG_GIF) {
     header("Content-type: image/gif");
     imagegif($this->img);
   } elseif (function_exists("imagejpeg")) {
     header("Content-type: image/jpeg");
     imagegif($this->img);
   } elseif (imagetypes() & IMG_PNG) {
     header("Content-type: image/png");
     imagegif($this->img);
   } else {
     die("No image support in this PHP server");
   } 
 
  }
 
  // Generate a verification code string 
  private function createcode() {
   $codes = "3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY";
 
   $code = "";
 
   for($i=0; $i < $this->num; $i++) {
    $code .=$codes{rand(0, strlen($codes)-1)}; 
   }
 
   return $code;
  }
 
  // Used to automatically destroy image resources 
  function __destruct() {
   imagedestroy($this->img);
  }
 
 }

In the above script, although there are many codes declaring the verification code class Vcode, the details are encapsulated in the class. As long as the object is directly output, a picture can be output to the client browser and used in the browser form. In addition, this class automatically gets the string in the verification code picture, which is facilitated in $_ SESSION ["code"] of the service. When submitting a form, the form can only be submitted successfully if the user enters the text displayed on the verification code picture in the form and it is exactly the same as the verification code string retained in the server. (Note: The verification code is in $_ SESSION ["code"] on the server side, so you must open an session session to use this class.)

In the following script code. php, use session_start () to open user session control, then include the file vcode. class. php where the verification code class Vcode resides, create the class object and output it directly. The randomly generated verification code picture can be sent out, and the verification code string will be automatically saved in the server for 1 copy. The code looks like this:


<?php
 // Open session
 session_start();
 include "vcode.class.php";
 // Construction method 
 $vcode = new Vcode(80, 30, 4);
 // Save the authentication code in the server's own space 1 Portions 
 $_SESSION['code'] = $vcode->getcode();
 // Output the verification code picture 
 $vcode->outimg();
?>

The form code looks like this:


<?php
 session_start();
if(isset($_POST['dosubmit'])) {
 if(strtoupper($_SESSION['code']) == strtoupper($_POST['code']) ) {
  echo " Successful input !<br>";
 }else{
  echo " Incorrect input !<br>";
 }
}
?>
 
<body>
 <form action="reg.php" method="post">
  username: <input type="text" name="username"> <br>
  password: <input type="password" name="password"> <br>
  code: <input type="text" onkeyup="if(this.value!=this.value.toUpperCase()) this.value=this.value.toUpperCase()" size="6" name="code"> 
    <img src="code.php" onclick="this.src='code.php?'+Math.random()" /> <br>
  <input type="submit" name="dosubmit" value=" Deng   Record "> <br>
 </form>
</body>

PHP Classic Verification Code Class Download:

PHP verification code class. rar


Related articles: