PHP verification code code (latest modification, fully customized!)_PHP tutorial

WBOY
Release: 2016-07-21 15:33:30
Original
961 people have browsed it

Authnum.class.php Download

Copy code The code is as follows:

session_start();
class Authnum {
//Image object, width, height, verification code length
private $im;
private $ im_width;
private $im_height;
private $len;
//Random string, y-axis coordinate value, random color
private $randnum;
private $y;
private $randcolor;
//The background color is red, green and blue, the default is light gray
public $red=238;
public $green=238;
public $blue=238;
/ **
* Optional settings: Verification code type, interference points, interference lines, Y-axis randomization
* Set to false to disable
**/
//The default is a mixture of uppercase and lowercase numbers, 1 2 3 represents lowercase, uppercase, and numeric respectively
public $ext_num_type='';
public $ext_pixel = false; //Interference point
public $ext_line = false; //Interference line
public $ext_rand_y= true; //Y-axis random
function __construct ($len=4,$im_width='',$im_height =25) {
// Verification code length, image width, and height are required data when instantiating the class
$this->len = $len; $im_width = $len * 15;
$ this->im_width = $im_width;
$this->im_height= $im_height;
$this->im = imagecreate($im_width,$im_height);
}
// Set the image background color, the default is light gray background
function set_bgcolor () {
imagecolorallocate($this->im,$this->red,$this->green,$this->blue ; an3 = '0123456789';
if ($this->ext_num_type == '') $str = $an1.$an2.$an3;
if ($this->ext_num_type == 1) $ str = $an1;
if ($this->ext_num_type == 2) $str = $an2;
if ($this->ext_num_type == 3) $str = $an3;
for ($i = 0; $i < $this->len; $i++) {
$start = rand(1,strlen($str) - 1);
$randnum .= substr( $str,$start,1);
}
$this->randnum = $randnum;
$_SESSION[an] = $this->randnum;
}
/ / Get the Y-axis of the verification code image
function get_y () {
if ($this->ext_rand_y) $this->y = rand(5, $this->im_height/5);
else $this->y = $this->im_height / 4 ;
}
// Get a random color
function get_randcolor () {
$this->randcolor = imagecolorallocate( $this->im,rand(0,100),rand(0,150),rand(0,200));
}
// Add interference points
function set_ext_pixel () {
if ($this ->ext_pixel) {
for($i = 0; $i < 100; $i++){
$this->get_randcolor();
imagesetpixel($this->im, rand()%100, rand()%100, $this->randcolor);
}
}
}
// Add interference line
function set_ext_line () {
if ($this->ext_line) {
for($j = 0; $j < 2; $j++){
$rand_x = rand(2, $this->im_width);
$rand_y = rand(2, $this->im_height);
$rand_x2 = rand(2, $this->im_width);
$rand_y2 = rand(2, $this-> im_height);
$this->get_randcolor();
imageline($this->im, $rand_x, $rand_y, $rand_x2, $rand_y2, $this->randcolor);
}
}
}
/**Create a verification code image:
* Create a canvas (__construct function)
* Set the canvas background ($this->set_bgcolor();)
* Get a random string ($this->get_randnum () ;)
* Write text to the image (imagestring function)
* Add interference points/lines ($this->set_ext_line(); $this->set_ext_pixel();)
* Output the image
**/
function create () {
$this->set_bgcolor();
$this->get_randnum ( );
for($i = 0; $i < $this->len; $i++){
$font = rand(4,6);
$x = $i/$ this->len * $this->im_width + rand(1, $this->len);
$this->get_y();
$this->get_randcolor();
imagestring($this->im, $font, $x, $this->y, substr($this->randnum, $i ,1), $this->randcolor);
}
$this->set_ext_line();
$this->set_ext_pixel();
header("content-type:image/png");
imagepng($this-> ;im);
imagedestroy($this->im); //Release image resources
}
}//end class
/**Methods to use the verification code class:
* $an = new Authnum (verification code length, image width, image height);
* When instantiated without parameters, the default is a four-digit regular size of 60*25 Verification code image
* Method to detect verification code on form page, compare whether $_SESSION[an] is equal to $_POST[Verification code text box ID]
* Optional configuration:
* 1. Verification code type: $an->ext_num_type=1; A value of 1 is a lowercase type, 2 is an uppercase type, and 3 is a numeric type
* 2. Interference point: $an->ext_pixel = false; A value of false means no interference is added. Point
* 3. Interference line: $an->ext_line = false; A value of false means no interference line will be added
* 4. Y-axis randomization: $an->ext_rand_y = false; A value of false means no interference line is added. The Y-axis randomization of the picture is not supported
* 5. Picture background: change the values ​​of the three member variables $red, green and $blue
**/
$an = new Authnum();
$an->ext_num_type='';
$an->ext_pixel = true ; //Interference point
$an->ext_line = false; //Interference line
$an->ext_rand_y= true; //Y-axis random
$an->green = 238;
$an->create();
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322609.htmlTechArticleAuthnum.class.php Download and copy the code. The code is as follows: ?php session_start(); class Authnum { //Image object , width, height, verification code length private $im; private $im_width; private...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template