We design a verification code class Vcode through PHP's GD library image processing content. Declare the class in file vcode.class.php and encapsulate some implementation details into the class with object-oriented attributes. You can successfully create an object of a validation code class whenever you create an object by providing three parameters for the constructor, including the width, height, and number of authentication code letters that create the CAPTCHA picture. The Declaration code for the class looks like this:
<?php class Vcode {private $width;//wide private $height;//high private $num; Number private $code; Authentication code private $IMG;
The resource//construction method of the image, three parameter function __construct ($width =80, $height =20, $num =4) {$this->width = $width;
$this->height = $height;
$this->num = $num; $this->code = $this->createcode ();
Call your Own method}//Get the character's captcha to be saved in the server function GetCode () {return $this->code;
//Output image function Outimg () {//Create background (color, size, border) $this->createback ();
Reprint (size, font color) $this->outstring ();
Interference elements (dots, lines) $this->setdisturbcolor ();
Output Image $this->printimg (); //Create background Private function Createback () {//create resource $this->img = Imagecreatetruecolor ($this->width, $this-&G
T;height);
Set a random background color $bgcolor = imagecolorallocate ($this->img, rand (255), Rand (255), rand (225, 255));
Set Background fill Imagefill ($this->img, 0, 0, $bgcolor); Draw Border $bordercolor = ImagecolorallocAte ($this->img, 0, 0, 0);
Imagerectangle ($this->img, 0, 0, $this->width-1, $this->height-1, $bordercolor); }//Reprint private Function outstring () {for ($i =0; $i < $this->num; $i + +) {$color = Imagecolorallocate ($th
Is->img, rand (0, 128), rand (0, 128), rand (0, 128)); $fontsize =rand (3,5); Font size $x = 3+ ($this->width/$this->num) * $i;
Horizontal position $y = rand (0, Imagefontheight ($fontsize)-3);
Draw each character Imagechar ($this->img, $fontsize, $x, $y, $this->code{$i}, $color); }//Set interference element Private function Setdisturbcolor () {//Plus points for ($i =0 $i <100; $i + +) {$color = Imagecolor
Allocate ($this->img, rand (0, 255), rand (0, 255), rand (0, 255));
Imagesetpixel ($this->img, rand (1, $this->width-2), rand (1, $this->height-2), $color); //Add line for ($i =0 $i <10; $i + +) {$color = Imagecolorallocate ($this->img, rand (0, 255), rand (0, 128), Rand
(0, 255)); Imagearc ($this->img,rand (-10), $this->width+10), rand ( -10, $this->height+10), Rand (a), Rand (a), 55,44, $color); ///Output image Private Function printimg () {if (Imagetypes () & Img_gif) {header ("Content-type:image/gif
");
Imagegif ($this->img);
} elseif (Function_exists ("imagejpeg")) {header ("content-type:image/jpeg");
Imagegif ($this->img);
} elseif (Imagetypes () & Img_png) {header ("content-type:image/png");
Imagegif ($this->img);
else {die ("No image support in this PHP server"); }//Generate verification Code string private function Createcode () {$codes = 3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUV
Wxy ";
$code = "";
For ($i =0 $i < $this->num; $i + +) {$code. = $codes {rand (0, strlen ($codes)-1)};
return $code;
///For automatic destruction of image resources function __destruct () {Imagedestroy ($this->img); }
}
In the script above, although the declaration of the Authentication Code class Vcode code is more, but the details are encapsulated in the class, as long as the direct output object, you can output a picture in the client browser, and can be used in browser forms. In addition, this class automatically obtains the string in the CAPTCHA image, which is facilitated by the $_session["code" of the service. When a form is submitted, the form can be submitted successfully only if the user enters the text displayed on the form in the verification code picture and is exactly the same as the authentication code string maintained in the server. (Note: The authentication code is on the server side in $_session["code"], so you must open session sessions to use this class.)
In the following script code.php, use Session_Start () to open user Session control, and then include the authentication code class Vcode The file vcode.class.php, create the class object and output directly. You can send a randomly generated verification code picture and automatically save the CAPTCHA string in a server. The code looks like this:
<?php
//Open session
session_start ();
Include "vcode.class.php";
The construction method
$vcode = new Vcode (4);
Will validate the stacking to the server own space to save a copy of
$_session[' code ' = $vcode->getcode ();
Output $vcode->outimg () of the Verification Code picture
;
? >
The form code looks like this:
<?php
session_start ();
if (Isset ($_post[' Dosubmit ')) {
if (strtoupper ($_session[' code ')) = = Strtoupper ($_post[' code ')) {
echo " Enter successful!<br> ";
} else{
echo "Input not!<br>";
}
? >
<body>
<form action= "reg.php" method= "POST" >
username: <input type= "text" Name= " Username "> <br>
Password: <input type=" password "name=" password "> <br>
Code: < Input type= "text" onkeyup= "if (This.value!=this.value.touppercase ()) this.value=this.value.touppercase ()" Size= "6" Name= "code" >
<br>
<input type=" Submit "Name=" Dosubmit "value=" Login "> <br>
</ Form>
</body>
PHP Classic Authentication code class download:
PHP Verification code class. rar