This article mainly introduces a useful PHP verification code Class example, the need for friends can refer to the
Share a handy PHP authentication code class, including the call example. Note: If you do not apply the specified font, then use the imagestring () function, and if you need to encounter the specified font, use the Imagettftext () function. The position of the font is windows/fonts under C disk. Reference to the online PHP generated verification code method, as well as PHP image verification code and PHP Chinese verification code generation method. Use of the PHP GD library related knowledge. 1, generate the Verification code class VerificationCode.class.php code as follows: <?php class verificationcode{ &nbs P Private $charset = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789"; //random factor private $code; //Verification Code Private $codelen = 4; Verification code length private $width = 110; Width private $height = 30; Height private $img; //Image resource handle private $font; //set font private $fontSize = 25; //font size private $fontColor; Font Color public Function __construct () {   $this->font= "Calibriz. TTF "; { //Generate verification code Private function Createcode () { $len =strlen ($this->charset)-1; & nbsp for ($i = 0 $i < $this->codelen; $i + +) { &NBS P $this->code. = $this->charset[mt_rand (0, $len)]; I Generate background Private Function createbg () { $ This->img=imagecreatetruecolor ($this->width, $this->height); $color = Imagecolorallocate ($this->img,mt_rand (157,255), Mt_rand ( 157,255), Mt_rand (157,255)); Imagefilledrectangle ($this->img,0, $this->height, $this->width,0, $color); { //Generate text Private function CreateFont () { $x = $this->width/$this->codelen; &nbs P for ($i = 0 $i < $this->codelen; $i + +) { &NBS P $this->fontcolor=imagecolorallocate ($this->img,mt_rand (0,156), Mt_rand (0,156), Mt_rand ( 0,156)); Imagettftext ($this->img, $this->fontsize,mt_rand (- 30,30), $i * $x +mt_rand (1,5), $this->height/1.4, $this->fontcolor, $this->font, $this->code[$i]); //www.jbxue.com //imagestring ($this->img,5, $i * $x +mt_ Rand (1,5), 5, $this->code[$i], $this->fontcolor); { } //Generate line, snowflake &NB Sp Private Function createdisturb () { for ($i = 0 ; $i < 6; $i + +) { $color =imagecolorallocate ($this->img,mt_rand 0, 156), Mt_rand (0,156), Mt_rand (0,156)); Imageline ($this->img,mt_rand (0, $this->width), Mt_ Rand (0, $this->width), Mt_rand (0, $this->width), Mt_rand (0, $this->width), $color); $i for ($i = 0; < 1 00; $i + +) { $color =imagecolorallocate ($this->img,mt_rand (20 0,255), Mt_rand (200,255), Mt_rand (200,255)); imagestring ($this->imG,mt_rand (1,5), Mt_rand (0, $this->width), Mt_rand (0, $this->height), ' * ', $color); I Output Private Function OutPut () { Heade R ("Content-type:image/png"); imagepng ($this->img); Imagedestroy ($this->img); { public Function ShowCode () { & nbsp $this->CREATEBG (); $this->createcode (); $this->createdisturb (); $this->createfont (); $this->output (); &NBsp //get validation code public Function GetCode () { & nbsp return Strtolower ($this->code); { } ?> code.php code as follows: <?p HP session_start (); require_once ' VerificationCode.class.php '; $code =new verificationcode (); $_session[' code ']= $code->getcode (); $code->showcode (); ?> Verification code: <input type= "text" name= "code"/><img src= "code.php" onclick= "javascript: This.src= ' code.php?time= ' +math.random (); "/>