Verification code class in PHP (perfect verification code)

Source: Internet
Author: User
Tags php server set background

Operation Result:

<!--vcode.class.php-->

<?php
Class Vcode {
Private $width; Wide
Private $height; High
Private $num; Number
Private $code; Verification Code
Private $img; Resources for images

Construction method, three parameters
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
}

Gets the verification code for the character to be saved in the server
function GetCode () {
return $this->code;
}

Output image
function outimg () {
Create background (color, size, border)
$this->createback ();

stooped (size, font color)
$this->outstring ();

Interference elements (points, lines)

$this->setdisturbcolor ();
Output image
$this->printimg ();
}

Create a background
Private Function Createback () {
Create a resource
$this->img = Imagecreatetruecolor ($this->width, $this->height);
Set a random background color
$bgcolor = Imagecolorallocate ($this->img, rand (225, 255), rand (225, 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);
}

Stooped
Private Function outstring () {
for ($i =0; $i < $this->num; $i +) {

$color = Imagecolorallocate ($this->img, rand (0, +), rand (0, +), rand (0, 128));

$fontsize =rand (3,5); Font size

$x = ($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);
}
}

Setting interference elements
Private Function Setdisturbcolor () {
Add points, 100 points
for ($i =0; $i <100; $i + +) {
$color = Imagecolorallocate ($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 lines, 10 lines
for ($i =0; $i <10; $i + +) {
$color = Imagecolorallocate ($this->img, rand (0, 255), rand (0, $), rand (0, 255));
Imagearc ($this->img,rand ( -10, $this->width+10), rand ( -10, $this->height+10), rand (+), rand (30, 300), 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 Authenticode String
Private Function Createcode () {
$codes = "3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY";

$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);
}

}

<!--reg.php-->

<?php
Session_Start ();
if (Isset ($_post[' Dosubmit ')) {
if (Strtoupper ($_session[' code ')) = = Strtoupper ($_post[' code ')) {
echo "Input success!<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>

<!--code.php Content--

<?php
Open session
Session_Start ();
Include "vcode.class.php";
Construction method
$vcode = new Vcode (80, 30, 4);
Save a copy of the validation to the server's own space
$_session[' Code ' = $vcode->getcode ();
The Captcha picture output
$vcode->outimg ();

Verification code class in PHP (perfect verification code)

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.