This article mainly introduces a practical PHP verification code class, has a certain reference value, interested in small partners can refer to, hope to help everyone.
Code.php is the verification code class, the name of the class is the best and the name of the filename, which is beneficial to our view.
code.php
<?phpheader (' Content-type:text/html;charset=utf8 '); class code{//Verification code number: protected $number; Verification code type $codetype protected $codeType; Verification Code Image width $width protected $width; Verification code $height protected $height; Verification code string $code protected $code; Image resources $image protected $image; Public function __construct ($number =4, $codeType =0, $height =50, $width =100) {//initializes its own member properties $this->number= $number; $this->codetype= $codeType; $this->width = $width; $this->height= $height; Generate the CAPTCHA function $this->code = $this->createcode (); Public Function __get ($name) {if ($name = = ' Code ') {return $this->code; } return false; }/* Get Verification Code */Public Function GetCode () {return $this->code; }/* Image resource destroyed */Public function __destruct () {Imagedestroy ($this->image); } protected function Createcode () {//Generate verification code via your CAPTCHA type switch ($this->codetype) {case 0://Pure Digital $code = $this->getnumbercode (); Break Case 1://plain Letter $code = $this->getcharcode (); Break Case 2://mixed numbers and letters $code = $this->getnumcharcode (); Break Default:die (' does not support this type of verification code '); } return $code; } protected function Getnumbercode () {$str = join (', range (0, 9)); Return substr (Str_shuffle ($STR), 0, $this->number); } protected function Getcharcode () {$str = join (', range (' A ', ' Z ')); $str = $str. Strtoupper ($STR); Return substr (Str_shuffle ($STR), 0, $this->number); } protected function Getnumcharcode () {$numstr = join (', range (0, 9)); $str =join (', Range (' A ', ' Z ')); $str = $numstr. $str. Strtoupper ($STR); Return substr (Str_shuffle ($STR), 0, $this->number); } protected function CreateImage () {$this->image = Imagecreatetruecolor ($this->width, $this->height); } protected function Fillback () {Imagefill ($this->image, 0, 0, $this->lightcolor ()); }/* Light */protected function Lightcolor () {return Imagecolorallocate ($This->image, Mt_rand (133,255), Mt_rand (133,255), Mt_rand (133,255)); }/* Dark */protected function Darkcolor () {return imagecolorallocate ($this->image, Mt_rand (0,120), Mt_rand (0,120), M T_rand (0,120)); } protected function Drawchar () {$width = Ceil ($this->width/$this->number); for ($i =0; $i < $this->number; $i + +) {$x = Mt_rand ($i * $width +5, ($i + 1) * $width-10); $y = Mt_rand (0, $this->height-15); Imagechar ($this->image, 5, $x, $y, $this->code[$i], $this->darkcolor ()); }} protected function DrawLine () {for ($i =0; $i <5; $i + +) {imageline ($this->image,mt_rand (0, $this->width ), Mt_rand (0, $this->height), Mt_rand (0, $this->width), Mt_rand (0, $this->height), $this->darkcolor ()); }} protected function Drawdisturb () {for ($i =0; $i <150; $i + +) {$x =mt_rand (0, $this->width); $y =mt_rand (0, $this->height); Imagesetpixel ($this->image, $x, $y, $this->lightcolor ()); }} protected FunctIon Show () {header (' content-type:image/png '); Imagepng ($this->image); The Public Function outimage () {//creates the canvas $this->createimage ();//fills the background color $this->fillback ();//takes the CAPTCHA string to On the canvas $this->drawchar ();//Add the interfering element $this->drawdisturb ();//Add Line $this->drawline ();//output and display $ This->show (); }}
Test.php is a new verification code and saves it to the session, saving and storing our verification code.
test.php
<?php//open Sessionsession_start (); require_once ' code.php '; $code = new Code (4,1,50,100); $_session[' Code ']= $code- >getcode (); $code->outimage ();
Login.php is the final verification.
login.php
<?php//Open session session_start (); Determine whether to commit the IF (Isset ($_post[' Dosubmit ')) {//Get the verification code in the SESSION and switch to lowercase $sessionCode =strtolower ($_session[' code '); Get the verification code entered $code =strtolower ($_post[' code '); Determine if equal if ($sessionCode = = $code) {echo "<script type= ' Text/javascript ' >alert" (' captcha correct! '); </script> "; }else{echo "<script type= ' text/javascript ' >alert (' captcha error! '); </script> "; }}?> <! DOCTYPE html>