PHP Verification Code Class code (the latest modification, fully customizable!) _php Tutorials

Source: Internet
Author: User
Authnum.class.php Download
Copy CodeThe code is as follows:
Session_Start ();
Class Authnum {
Picture object, width, height, captcha length
Private $im;
Private $im _width;
Private $im _height;
Private $len;
Random string, y coordinate value, random color
Private $randnum;
Private $y;
Private $randcolor;
Red, green and blue background color, light gray by default
Public $red = 238;
Public $green = 238;
Public $blue = 238;
/**
* Optional Settings: Verification code type, interference point, interference line, y-axis random
* Set to False indicates not enabled
**/
The default is case-number mixed, 1 2 3 for lowercase, uppercase, and numeric
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, height is the data required to instantiate 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 picture background color, default is light gray background
function Set_bgcolor () {
Imagecolorallocate ($this->im, $this->red, $this->green, $this->blue);
}
Get random numbers of arbitrary digits
function Get_randnum () {
$an 1 = ' abcdefghijklmnopqrstuvwxyz ';
$an 2 = ' abcdefghijklmnopqrstuvwxyz ';
$an 3 = ' 0123456789 ';
if ($this->ext_num_type = = ") $str = $an 1. $an 2. $an 3;
if ($this->ext_num_type = = 1) $str = $an 1;
if ($this->ext_num_type = = 2) $str = $an 2;
if ($this->ext_num_type = = 3) $str = $an 3;
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 Verification code Picture Y axis
function get_y () {
if ($this->ext_rand_y) $this->y = rand (5, $this-&GT;IM_HEIGHT/5);
else $this->y = $this-&GT;IM_HEIGHT/4;
}
Get random colors
function Get_randcolor () {
$this->randcolor = imagecolorallocate ($this->im,rand (0,100), Rand (0,150), Rand (0,200));
}
Adding interference points
function Set_ext_pixel () {
if ($this->ext_pixel) {
for ($i = 0; $i < $i + +) {
$this->get_randcolor ();
Imagesetpixel ($this->im, rand ()%100, Rand ()%100, $this->randcolor);
}
}
}
Adding interference lines
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 CAPTCHA Image:
* Create canvas (__construct function)
* Set Canvas background ($this->set_bgcolor ();)
* Get random string ($this->get_randnum ();)
* Text is written to the picture (imagestring function)
* Add interference points/lines ($this->set_ext_line (); $this->set_ext_pixel ();)
* Output 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); Releasing image resources
}
}//end class
/** methods for using CAPTCHA classes:
* $an = new Authnum (Verification code length, image width, image height);
* The default is four-bit 60*25 size general code image when instantiated without parameters
* Method of checking Verification code on form page, compare $_session[an] equals $_post[captcha 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 points are added
* 3. Interference line: $an->ext_line = false; A value of false indicates that no interference lines are added
* 4.Y Axis random: $an->ext_rand_y = false; A value of false indicates that the picture y-axis is not supported randomly
* 5. Picture background: Change the value of $red $green $blue three member variables
**/
$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 ();
?>

http://www.bkjia.com/PHPjc/322609.html www.bkjia.com true http://www.bkjia.com/PHPjc/322609.html techarticle Authnum.class.php Download Copy code code as follows: PHP session_start (); class Authnum {//Picture object, Width, height, captcha length private $im; private $im _widt H Private ...

  • 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.