A very useful PHP verification code class, code and how to use

Source: Internet
Author: User

Code for CAPTCHA class: Validationcode

<?php
Class Validationcode {
Private $width;
Private $height;
Private $codeNum;
Private $image; Image Resources
Private $disturbColorNum;
Private $checkCode;

function __construct ($width =80, $height =20, $codeNum =4) {
$this->width= $width;
$this->height= $height;
$this->codenum= $codeNum;
$this->checkcode= $this->createcheckcode ();
$number =floor ($width * $height/15);

if ($number > 240-$codeNum) {
$this->disturbcolornum= 240-$codeNum;
}else{
$this->disturbcolornum= $number;
}


}
Output an image to the browser by accessing the method
function ShowImage ($fontFace = "") {
First step: Create an image background
$this->createimage ();
Step two: Set the interference element
$this->setdisturbcolor ();
Step three: Randomly draw text into an image
$this->outputtext ($fontFace);
Fourth Step: Output image
$this->outputimage ();
}

Get a randomly created captcha string by calling this method
function Getcheckcode () {
return $this->checkcode;
}

Private Function CreateImage () {
Create an image resource
$this->image=imagecreatetruecolor ($this->width, $this->height);
Random background color
$backColor =imagecolorallocate ($this->image, rand (225, 255), Rand (225,255), rand (225, 255));
Fills the background with color
Imagefill ($this->image, 0, 0, $backColor);
Set Border color
$border =imagecolorallocate ($this->image, 0, 0, 0);
Draw a rectangular border
Imagerectangle ($this->image, 0, 0, $this->width-1, $this->height-1, $border);
}

Private Function Setdisturbcolor () {
for ($i =0; $i < $this->disturbcolornum; $i +) {
$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);
}

for ($i =0; $i <10; $i + +) {
$color =imagecolorallocate ($this->image, rand (255), Rand (255), rand (200, 255));
Imagearc ($this->image, rand ( -10, $this->width), rand ( -10, $this->height), rand (+), rand (20, 200), 55, 44, $color);
}
}

Private Function Createcheckcode () {
$code = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
$string = ";
for ($i =0; $i < $this->codenum; $i +) {
$char = $code {rand (0, strlen ($code)-1)};
$string. = $char;
}

return $string;
}

Private Function outputtext ($fontFace = "") {
for ($i =0; $i < $this->codenum; $i +) {
$fontcolor =imagecolorallocate ($this->image, rand (0, +), rand (0, $), rand (0, 128));
if ($fontFace = = "") {
$fontsize =rand (3, 5);
$x =floor ($this->width/$this->codenum) * $i +3;
$y =rand (0, $this->height-15);
Imagechar ($this->image, $fontsize, $x, $y, $this->checkcode{$i}, $fontcolor);
}else{
$fontsize =rand (12, 16);
$x =floor (($this->width-8)/$this->codenum) * $i +8;
$y =rand ($fontSize +5, $this->height);
Imagettftext ($this->image, $fontsize, Rand ( -30,), $x, $y, $fontcolor, $fontFace, $this->checkcode{$i});
}
}
}

Private Function Outputimage () {
if (Imagetypes () & Img_gif) {
Header ("Content-type:image/gif");
Imagepng ($this->image);
}else if (Imagetypes () & img_jpg) {
Header ("Content-type:image/jpeg");
Imagepng ($this->image);
}else if (Imagetypes () & Img_png) {
Header ("Content-type:image/png");
Imagepng ($this->image);
}else if (Imagetypes () & Img_wbmp) {
Header ("Content-type:image/vnd.wap.wbmp");
Imagepng ($this->image);
}else{
Die ("PHP does not support image creation");
}
}

function __destruct () {
Imagedestroy ($this->image);
}
}

Using code.php to enter a verification code

<?php
Session_Start ();
Include "validationcode.class.php";

$code =new Validationcode (100, 100, 4);//Three numbers are long, wide, and the number of verification codes that appear

$code->showimage (); Output to the page for registration or sign-in use

$_session["code"]= $code->getcheckcode (); Save the CAPTCHA to the server

Use login.php in the sign-in page

<?php
Session_Start ();
echo $_post["code"]. " <br> ";
echo $_session["code"]. " <br> ";
For verification Code input judgment, of course, you can use Ajax to judge
if (Strtoupper ($_post["code"]) ==strtoupper ($_session["code")) {
echo "OK";
}else{
echo "Error";
}
?>
<body>
<form action= "login.php" method= "POST" >
User:<input type= "text" name= "Usenrame" ><br>
Pass:<input type= "PASSOWRD" name= "Pass" ><br>

Code: <input type= "text" name= "code" onkeyup= "if (This.value!=this.value.touppercase ()) this.value= This.value.toUpperCase () "> <br>
<!--above this code for reference code.php for verification code input, and to achieve click on the picture to change the image of the function, and the use of JS control case-
<input type= "Submit" Name= "sub" value= "Login" ><br>
</form>
</body>

A very useful PHP verification code class, code and how to use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.