An instance of Captcha verification code class implemented by php
- 2021-07-18 07:35:04
- OfStack
This paper describes the Captcha verification code class implemented by php, which is widely used in php programming. Share it for your reference. The specific methods are as follows:
The verification code class 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 picture
* @param int $length Verification code length
* @param Array $param In fact, in fact, the
* @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; // Number of interfering pixels
$lnum = isset($param['lnum'])? $param['lnum'] : 2; // Number of interfering 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 with the size of x_size And y_size Gets or sets a blank image of the.
$black = ImageColorAllocate($im, 238,238,238); // Set the 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
/* Text */
for ($i = 0; $i < strlen($authnum); $i++){
$font = ImageColorAllocate($im, mt_rand(0,50),mt_rand(0,150),mt_rand(0,200));// Set the 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 interfering pixels */
for($i=0; $i<$pnum; $i++){
$dist = ImageColorAllocate($im, mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); // Set Miscellaneous 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); // With PNG Format to output images to browsers or files
ImageDestroy($im); // Destruction 1 Image
}
/** Check the verification code
* @param String $captcha Verification code
* @param int $flag After successful verification, 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)){ // Detection verification code
if($flag==1){
$this->captcha_session($this->sname,'');
}
return true;
}else{
return false;
}
}
}
/* Generate random number function
* @param int $length A randomly generated string is required
* @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 Treatment 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 For saving captcha Adj. session name, Can be left blank , Leave blank as 'm_captcha'
$obj->create($length,$param); # Create Captcha And output pictures
# $length For 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
)
Can be left blank
*/
$obj->check($captcha,$flag); # Check whether the verification code entered by the user is correct, true or false
# $captcha Verification code entered for the user , Required
# $flag Can be left blank, default to 1
# 1: Automatically clear when verification is successful captcha session
# 0: Do not clear after successful block verification captcha session, Used for ajax Check
?>
I believe that this paper has a certain reference value for everyone's study of php programming.