CAPTCHA Verification Code class instance _php techniques implemented in PHP

Source: Internet
Author: User
Tags rand set background

This article describes the PHP implementation of the CAPTCHA authentication code class, in the PHP program design has a very wide range of applications. Share to everyone for your reference. The specific methods are as follows:

The Authentication code class file is as follows:

<?php/** Captcha Authentication 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 a CAPTCHA picture * @param int $length Verification Code length * @param Array $param parameters * @return IMG/Public Function Create ($length =4
 , $param =array ()) {Header ("content-type:image/png"); $authnum = $this->random ($length);
 
 Generates a CAPTCHA character. $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; Number of jamming pixels $lnum = isset ($param [' lnum '])? $param [' Lnum ']: 2;  Number of interference lines $this->captcha_session ($this->sname, $authnum);
 Writes a random number to the session $PW = $width * $length +10;
  
 $PH = $height +6;   $im = Imagecreate ($PW, $ph);
 Imagecreate () Creates a new image with a blank image size of 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 interference Polygon Bottom chart * * Text/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 the random x-coordinate $y = rand (1, $PH/3); 
 Set random y-coordinate imagestring ($im, Mt_rand (4,6), $x, $y, substr ($authnum, $i, 1), $font); }/* Add the jamming pixel */for ($i =0; $i < $pnum; $i + +) {$dist = Imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0, 255)); 
 Set the miscellaneous point color Imagesetpixel ($im, Mt_rand (0, $PW), Mt_rand (0, $ph), $dist); /* Add the Jamming 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); Output the image to the browser or file Imagedestroy ($im) in PNG format; 
 Destroy an image}/** check Verification code * @param String $captcha Verification Code * @param int $flag validation successful 0: Do not clear session 1: Clear Session * @return Boolean
 */Public function check ($captcha, $flag =1) {if (empty ($captcha)) {return false; }else{if (strtoupper ($captcha) = = $this->captcha_session ($this->sname) {//Detect Verification code if ($flag ==1) {$this-&GT;CAPTC
  Ha_session ($this->sname, ");
  return true;
  }else{return false; }}/* Generate random number function * @param int $length need randomly generated string numbers * @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; /** Validation 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 (' Captcha.class.php '); 
 
  $obj = new Captcha ($sname);   # Create Captcha Class object 
                  # $sname to save the session name for CAPTCHA, leave it blank, and leave it blank for ' M_captcha ' 
 
  $obj->create ($length, $param);  #创建Captcha并输出图片 
                  # $length for Captcha length, can be left blank, defaults to 4 
                  /* $param = Array ( 
                      ' width ' =>    captcha character width 
                      ' Height ' =>    captcha character height 
                      ' pnum ' =>    number of interference points
                      ' Lnum ' => 2     interference lines 
                      can be left blank 
                  * * 
  $obj->check ($captcha, $flag); # Check that the user entered the validation code correctly, True or False 
                  # $captcha The authentication code entered for the user, required 
                  # $ Flag can be left blank, the default is 1  
                  #    1: Automatically clear captcha session # 0 after successful validation 
                  : Captcha session is    not cleared after successful verification, for Ajax checking 
?>

I believe this article describes the learning of PHP program design for a certain reference value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.