PHP Production Verification Code, PHP verification code
PHP Production Verification Code detailed tutorial
Effect:
Myvcode.class.php: Encapsulates a class that creates a verification code
1:
2:/*
3:* file:myvcode.class.php
4:* Verification code class, class name Vcode
5:* /
6:class Vcode
7: {
8: private $width; / * Verification Code Width * /
9: private $height; / * Verify Code Height * /
Ten: private $codeNum; / * Number of verification code characters * /
One : private $checkCode; / * Verification Code character * /
: private $image; / * Verification Code resource * /
: private $pixNum; / * Draw the number of interference points * /
: private $lineNum; / * Draw the number of lines of interference * /
15:
: /*
# : * Construct a method to instantiate the Captcha object and initialize the data
* : * @param int $width Set default width
* : * @param int $height set default height
* : * @param int $codeNum Set the number of characters in the verification code
* : * @param int $pixNum Set the number of interference points
Total : * @param int $lineNum Set the number of interference lines
At: * /
: function __construct ($width =80, $height =40, $codeNum =4, $pixNum =40, $lineNum =5)
: {
: $this->width = $width;
->height : $this = $height;
: $this->codenum = $codeNum;
£ º $this->pixnum = $pixNum;
: $this->linenum = $lineNum;
: }
: / * Internal private method, create image resource * /
: Private function getcreateimage ()
: {
: $this->image = Imagecreatetruecolor ($this->width, $this->height);
: $white = imagecolorallocate ($this->image,0xff,0xff,0xff);
Panax Notoginseng: imagefill ($this->image, 0, 0, $white);
A : $black = imagecolorallocate ($this->image,0,0,0);
: imagerectangle ($this->image, 0, 0, $this->width-1, $this->height-1, $black);
Max : }
: /* Internal private method, draw characters, remove O0llz and 012*/
£ º Private function Createcheckcode ()
: {
: ' 3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKMNPQRSTUVWXY ';
: "";
: for ($i =0; $i < $this->codenum; $i +)
: {
: $char = $code {rand (0,strlen ($code)-1)};
->checkcode : $this. = $char;
: $fontColor = imagecolorallocate ($this->image, rand (0,128), Rand (0,128), Rand (0,128));
Wuyi: $fontSize = rand (3,5);
: $x = rand (0, $this->width-imagefontwidth ($fontSize));
: $y = rand (0, $this->height-imagefontheight ($fontSize));
Wu : Imagechar ($this->image, $fontSize, $x, $y, $char, $fontColor);
: }
: }
£ º / * Internal Private method Set interference element * /
: Private function Setdisturbcolor ()
: {
: / * Draw a disturbance point * /
A : for ($i =0; $i < $this->pixnum; $i +)
: {
A : $color = imagecolorallocate ($this->image, rand (0,255), Rand (0,255), Rand (0,255));
: imagesetpixel ($this->image, rand (1, $this->width-2), rand (1, $this->height-2), $ color);
: }
: / * Draw an interference line * /
: for ($i =0; $i < $this->linenum; $i +)
: {
A : $color = imagecolorallocate ($this->image, rand (0,255), Rand (0,255), Rand (0,255));
: imageline ($this->image, rand (1, $this->WIDTH/2), rand (1, $this->HEIGHT/2),
Rand ($this->WIDTH/2, $this->width–2), rand ($this->HEIGHT/2, $this->height–2), $color);
: }
: }
: / * Open session Save with ECHO output image * /
About : function __tostring ()
: {
: $_session[' code '= Strtoupper ($this->checkcode);
: $this->getcreateimage ();
->createcheckcode : $this ();
->setdisturbcolor : $this ();
->outputimg : $this ();
Bayi: }
: / * Internal Private method Output image * /
: Private function outputimg ()
A : {
: header ("Content-type:image/png");
: imagepng ($this->image);
: }
: / * destructor, Release object * /
: function __destruct ()
: {
: Imagedestroy ($this->image);
: }
: }
94: ?>
imgcode.php Output Image
1:
2: session_start ();
3:require_once(' myvcode.class.php ');
4:echonew vcode ();
5: ?>
test.html: same img Tag reference
1:<imgsrc= "imgcode.php">
Can add a tag, with JS to achieve a change effect:
/* Local Refresh Verification Code */function ChangeCode () {var imgcode = document.getElementById (' code '); var change = document.getElementById ( ' Change '); Change.onclick = function () {/* must add the following parameters to refresh */imgcode.src= ' Code.php?tm ' +math.random ()}}
Code and change are the IDs of IMG and a, respectively.
PHP Verification Code production problems
Very simply, when you use [pic] PHP checks to see if there is a constant named pic. This must not be found, so it outputs a warning message at the beginning of the file. This information is output before the image data, which prevents the image from being recognized by the browser. The workaround is to enclose the pic in quotation marks, $_session[' pic ']
PHP Image Verification Code implementation
can be used in PHP's GD library to do
Randomly generated verification code
Class RandomString
{
function Createrandomstr ($strLen)
{
List ($usec, $sec) = explode (' ', Microtime ());
(float) $sec + ((float) $usec * 100000);
$number = ";
$number _len = $strLen;
$stuff = ' 1234567890abcdefghijklmnopqrstuvwxyz ';//additional Code display range ABCDEFGHIJKLMNOPQRSTUVWXYZ
$stuff _len = strlen ($stuff)-1;
for ($i = 0; $i < $number _len; $i + +) {
$number. = substr ($stuff, Mt_rand (0, $stuff _len), 1);
}
return $number;
}
}
Convert the captcha into a picture via the ZD library
$number = $createStr->createrandomstr (' 4 ');//number of digits of the verification code
$number _len = strlen ($number);
$_session["Verify_code"] = $number;
Generate a Captcha picture
$img _width = 60;
$img _height = 20;
$img = Imagecreate ($img _width, $img _height);
Imagecolorallocate ($img, 0x6c, 0x74, 0x70);
$white = Imagecolorallocate ($img, 0xFF, 0xFF, 0xff);
$ix = 6;
$iy = 2;
for ($i = 0; $i < $number _len; $i + +) {
Imagestring ($img, 5, $ix, $iy, $number [$i], $white);
$ix + = 14;
}
for ($i =0; $i <200; $i + +)//Add interfering pixels
{
$randcolor = Imagecolorallocate ($img, Rand (0,255), Rand (0,255), Rand (0,255));
Imagesetpixel ($img, Rand ()%100, Rand ()%50, $randcolor);
}
Output picture
Header ("Content-type:".) Image_type_to_mime_type (imagetype_png));
Imagepng ($IMG);
Imagedestroy ($img); .... Remaining full text >>
http://www.bkjia.com/PHPjc/890823.html www.bkjia.com true http://www.bkjia.com/PHPjc/890823.html techarticle PHP Production Verification Code, PHP Verification code PHP Production Verification Code detailed tutorial effect: myvcode.class.php: Package to create a verification code Class 1:? PHP 2:/* 3: * file:myvcode.class.php 4: * Verification code class ...