Example analysis of CAPtcha of php ci framework

  • 2020-06-23 00:01:01
  • OfStack

php code:

<?php 
class Captcha_code
{
 var $width='60';
 var $num='4';
 var $height='20';
 var $name='randcode';
 public function __construct($conf="")
 {
  if($conf!="")
  {
   foreach($conf as $key=>$value)
   {
    $this->$key=$value;
   }
  }
 }

 function show()
 {
  Header("Content-type: image/gif");
  /*
  *  Initialize the 
  */
  $border = 0; // Whether you want a border  1 to :0 Don't 
  $how = $this->num; // Verification code number 
  $w = $this->width; // Image width 
  $h = $this->height; // Picture height 
  $fontsize = 5; // The font size 
  $alpha = "abcdefghijkmnopqrstuvwxyz"; // Captcha content 1: The letter 
  $number = "023456789"; // Captcha content 2: digital 
  $randcode = ""; // Validation code string initialization 
  srand((double)microtime()*1000000); // Initializes the random number seed 

  $im = ImageCreate($w, $h); // Create a verification image 

  /*
  *  Draw the basic frame 
  */
  $bgcolor = ImageColorAllocate($im, 255, 255, 255); // Set the background color 
  ImageFill($im, 0, 0, $bgcolor); // Fill background color 
  if($border)
  {
   $black = ImageColorAllocate($im, 0, 0, 0); // Set the border color 
   ImageRectangle($im, 0, 0, $w-1, $h-1, $black);// Draw the border 
  }

  /*
  *  Produces random characters bit by bit 
  */
  for($i=0; $i<$how; $i++)
  {   
   $alpha_or_number = mt_rand(0, 1); // Letters or Numbers 
   $str = $alpha_or_number ? $alpha : $number;
   $which = mt_rand(0, strlen($str)-1); // Which character to take 
   $code = substr($str, $which, 1); // Take a character 
   $j = !$i ? 4 : $j+15; // Glyph position 
   $color3 = ImageColorAllocate($im, mt_rand(0,100), mt_rand(0,100), mt_rand(0,100)); // Character random color 
   ImageChar($im, $fontsize, $j, 3, $code, $color3); // Draw characters 
   $randcode .= $code; // Add the captcha string bit by bit 
  }

  /*
  *  Add interference 
  */
  for($i=0; $i<5; $i++)// Draw background interference lines 
  {   
   $color1 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); // Interference line color 
   ImageArc($im, mt_rand(-5,$w), mt_rand(-5,$h), mt_rand(20,300), mt_rand(20,200), 55, 44, $color1); // Interference lines 
  }   
  for($i=0; $i<$how*15; $i++)// Paint the background noise 
  {   
   $color2 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); // Interference point color  
   ImageSetPixel($im, mt_rand(0,$w), mt_rand(0,$h), $color2); // Noise points 
  }

  // Writes the captcha string session

  //$this->session->set_userdata(array($this->name=>$randcode));

  $_SESSION[$this->name]=$randcode;
  /* End of the drawing */
  Imagegif($im);
  ImageDestroy($im);
  /* End of the drawing */
 }
}
?>

Call php code:

    function verify_image() {
        $conf['name'] = 'verify_code'; // As a configuration parameter 
        $this->load->library('lib_captcha', $conf);
        $this->lib_captcha->show();
        $yzm_session = $this->session->userdata('verify_code');
        echo $yzm_session;
    }

html code:

<dl>
          <dt> Verification code: </dt>
          <dd>
            <input type="text" name="verify_text" id="verify_text" class="yzma" value="">
            <img src="/user/verify_image" alt=" Verification code " id="verify_code" class="yz_img" />
            <a href="javascript:changeCode();" class="change_yz"> in 1 zhang </a>
          </dd>
          <dd class="tips_wrong"><b> The captcha is incorrect </b></dd>
          <dd class="tips_correct"></dd>
        </dl>

js code:

<script type="text/javascript">
    function changeCode(){
         FS.query("#verify_code").src ="/user/verify_image?r=" + Math.random();
    }
</script>


Related articles: