PHP implementation Click to refresh the verification code, PHP implementation Refresh verification code
Verification code class file CreateImg.class.php
<?php class Validationcode {private $width, $height, $codenum; public $checkcode; The generated verification code private $checkimage; Verification code Picture Private $disturbColor = '; Interference pixel function __construct ($width = ' a ', $height = ' ", $codenum = ' 4 ') {$this->width= $width; $this->height= $height; $this->codenum= $codenum; } function Outimg () {//Output header $this->outfileheader (); Generate Verification Code $this->createcode (); Produce picture $this->createimage (); Set the interference pixel $this->setdisturbcolor (); Write the verification code on the picture $this->writecheckcodetoimage (); Imagepng ($this->checkimage); Imagedestroy ($this->checkimage); } Private Function Outfileheader () {Header ("Content-type:image/png");} Private Function Createcode () {$this->checkcode = Strtoupper (substr (MD5 (rand ()), 0, $this->codenum));} Private Function CreateImage () {$this->checkimage = @imagecreate ($this->width, $this->height); $back = Imagecolorallocate ($this->checkimage,255,255,255); $border = Imagecolorallocate ($this->checkimage,0,0,0); Imagefilledrectangle ($this->checkimage,0,0, $this->width-1, $this->height-1, $back); White Bottom Imagerectangle ($this->checkimage,0,0, $this->width-1, $this->height-1, $border); Black Border} Private function Setdisturbcolor () {for ($i =0; $i <=200; $i + +) {$this->disturbcolor = Imagecolorallo Cate ($this->checkimage, rand (0,255), Rand (0,255), Rand (0,255)); Imagesetpixel ($this->checkimage,rand (2,128), Rand (2,38), $this->disturbcolor); }} Private Function Writecheckcodetoimage () {for ($i =0; $i <= $this->codenum; $i + +) {$BG _color = Imagecolorall Ocate ($this->checkimage, rand (0,255), Rand (0,128), Rand (0,255)); $x = Floor ($this->width/$this->codenum) * $i; $y = rand (0, $this->height-15); Imagechar ($this->checkimage, rand (5,8), $x, $y, $this->checkcode[$i], $bg _color); }} function __destruct () {unset ($this->width, $this->height, $this->codenum);}} ?>
Include File imgcode.php
<?php session_start (); Require_once (' CreateImg.class.php '); $image = new Validationcode (' + ', ' 4 '); Picture length, width, number of characters $image->outimg (); $_session[' validationcode ' = $image->checkcode; Storage verification code to $_session?>
Front desk File demo.php
? php session_start (); $test = $_post[' test ']; $test = Strtoupper (Trim ($test)); $submit = $_post[' Submit ']; if (Isset ($submit)) {if ($test ==$_session[' Validationcode ') { echo ' true ';} else { echo ' false ';}}?> Image
The above is for everyone to introduce the PHP click Verification code can be refreshed all the code, I hope that everyone's learning has helped.
http://www.bkjia.com/PHPjc/1068820.html www.bkjia.com true http://www.bkjia.com/PHPjc/1068820.html techarticle PHP Implementation Click to refresh the verification code, PHP implementation Refresh code Verification code class file CreateImg.class.php PHP class Validationcode {Private $width, $height, $codenum; Public $checkcode; /...