SoFunction
Updated on 2025-03-04

Captcha verification code class instance implemented by php

This article describes the Captcha verification code class implemented by php, which has extremely wide applications in php programming. Share it for your reference. The specific methods are as follows:

The verification code file is as follows:

<?php
/** Captcha verification code class
 * Date: 2011-02-19
 * Author: fdipzone
 */

class Captcha{ //class start

 private $sname = '';

 public function __construct($sname=''){ // $sname captcha session name
 $this->sname = $sname==''? 'm_captcha' : $sname;
 }

 /** Generate verification code pictures
  * @param int $length Verification code length
  * @param Array $param parameter
  * @return IMG
  */
 public function create($length=4,$param=array()){
 Header("Content-type: image/PNG");
 $authnum = $this->random($length); //Generate verification code characters. 
 $width = isset($param['width'])? $param['width'] : 13; //Text width $height = isset($param['height'])? $param['height'] : 18; //Text height $pnum = isset($param['pnum'])? $param['pnum'] : 100; //The number of interfering pixels $lnum = isset($param['lnum'])? $param['lnum'] : 2; //The number of interference lines
 $this->captcha_session($this->sname,$authnum);  //Write random numbers to session
 $pw = $width*$length+10;
 $ph = $height+6;
  
 $im = imagecreate($pw,$ph);   //imagecreate() Create a new image, a blank image of sizes x_size and y_size. $black = ImageColorAllocate($im, 238,238,238); //Set background color 
 $values = array(
  mt_rand(0,$pw), mt_rand(0,$ph),
  mt_rand(0,$pw), mt_rand(0,$ph),
  mt_rand(0,$pw), mt_rand(0,$ph),
  mt_rand(0,$pw), mt_rand(0,$ph),
  mt_rand(0,$pw), mt_rand(0,$ph),
  mt_rand(0,$pw), mt_rand(0,$ph)
 );
 imagefilledpolygon($im, $values, 6, ImageColorAllocate($im, mt_rand(170,255),mt_rand(200,255),mt_rand(210,255))); //Set the interference polygon base map 
 /* Word */
 for ($i = 0; $i < strlen($authnum); $i++){
  $font = ImageColorAllocate($im, mt_rand(0,50),mt_rand(0,150),mt_rand(0,200));//Set text color  $x = $i/$length * $pw + rand(1, 6); //Set random X coordinates  $y = rand(1, $ph/3);   //Set random Y coordinates  imagestring($im, mt_rand(4,6), $x, $y, substr($authnum,$i,1), $font); 
 }

 /* Add interference pixels */
 for($i=0; $i<$pnum; $i++){
  $dist = ImageColorAllocate($im, mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //Set the dot color  imagesetpixel($im, mt_rand(0,$pw) , mt_rand(0,$ph) , $dist); 
 } 

 /* Add interference line */
 for($i=0; $i<$lnum; $i++){
  $dist = ImageColorAllocate($im, mt_rand(50,255),mt_rand(150,255),mt_rand(200,255)); //Set line color  imageline($im,mt_rand(0,$pw),mt_rand(0,$ph),mt_rand(0,$pw),mt_rand(0,$ph),$dist);
 }

 ImagePNG($im); //Export the image to the browser or file in PNG format ImageDestroy($im); //Destroy an image }

 /** Check the verification code
  * @param String $captcha Verification Code
  * @param int $flag After verification is successful 0: session not cleared 1: session cleared
  * @return boolean
  */ 
 public function check($captcha,$flag=1){
 if(empty($captcha)){
  return false;
 }else{
  if(strtoupper($captcha)==$this->captcha_session($this->sname)){ //Detection verification code  if($flag==1){
   $this->captcha_session($this->sname,'');
  }
  return true;
  }else{
  return false;
  }
 }
 }

 /* Generate random number function
  * @param int $length Number of strings that need to be generated randomly
  * @return String
  */
 private function random($length){
 $hash = '';
 $chars = 'ABCDEFGHIJKLMNPQRSTUVWXYZ23456789';
 $max = strlen($chars) - 1;
 for($i = 0; $i < $length; $i++) {
  $hash .= $chars[mt_rand(0, $max)];
 }
 return $hash;
 }

 /** Verification code session processing method
  * @param String $name captcha session name
  * @param String $value
  * @return String
  */
 private function captcha_session($name,$value=null){
 if(isset($value)){
  if($value!==''){
  $_SESSION[$name] = $value;
  }else{
  unset($_SESSION[$name]);
  }
 }else{
  return isset($_SESSION[$name])? $_SESSION[$name] : '';
 }
 }
} // class end
?>

The demo sample program is as follows:

<?php 
  session_start(); 
  require_once(''); 
 
  $obj = new Captcha($sname);   # Create Captcha class object                  # $sname is the session name that saves captcha, can be left blank, and the space is 'm_captcha' 
  $obj->create($length,$param);  #Create Captcha and output pictures                  # $length is Captcha length, can be left blank, default is 4                  /* $param = array(
                       'width' => 13 captcha character width
                       'height' => 18 captcha character height
                       'pnum' => 100 Number of interference points
                       'lnum' => 2 Number of interfering lines
                       )
                       Leave it empty
                   */ 
  $obj->check($captcha,$flag); # Check whether the verification code entered by the user is correct, true or false                  # $captcha is the verification code entered by the user, required                  # $flag can be left blank, default is 1                  #1: Automatically clear captcha session after verification is successful                  # 0: Captcha session is not cleared after successful block verification, used for ajax check?>

I believe that the description in this article will have certain reference value for everyone's learning of PHP programming.