PHP Verification Code Generator Program code

Source: Internet
Author: User

PHP Verification code and generate a picture program, using the session recognition, slightly improved the current network of PHP verification code circulated, add miscellaneous points, digital color random display, control 4 digits display; Words are not much said, the procedure is as follows, share out.

New yz.php Authentication code generation file:

Note: The following code needs to open the GD Library of PHP, modify the configuration of the php.in file, the line has been commented out before the semicolon cancellation: Extension=php_gd2.dll.

The code is as follows Copy Code

<?php
Class Validationcode
{
Property
Private $width;
Private $height;
Private $codeNum;
Private $image;
Private $disturbColorNum; Number of interfering elements
Private $checkCode;
function __construct ($width =80, $height =20, $codeNum =4)
{
$this->width= $width;
$this->height= $height;
$this->codenum= $codeNum;
$number =floor ($width * $height/15);
if ($number >240-$codeNum)
{
$this->disturbcolornum=240-$codeNum;
}else
{
$this->disturbcolornum= $number;
}
$this->checkcode= $this->createcheckcode ();
}
function Getcheckcode ()
{
return $this->checkcode;
}
Private Function CreateImage () {
$this->image=imagecreatetruecolor ($this->width, $this->height);
$backcolor =imagecolorallocate ($this->image,rand (225,255), Rand (225,255), Rand (255,255));
Imagefill ($this->image,0,0, $backcolor);
$border =imagecolorallocate ($this->image,0,0,0);
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 (0,255), Rand (0,255), Rand (0,255));
Imagearc ($this->image,rand ( -10, $this->width), rand ( -10, $this->height), rand (30,300), Rand (20,300), 55, 44, $color);
}
}
Private Function outputtext ($fontFace = "") {
For ($i =0 $i < $this->codenum; $i + +)
{
$fontcolor =imagecolorallocate ($this->image,rand (0,128), Rand (0,128), Rand (0,128));
if ($fontFace = = "")
{
$fontsize =rand (3,5);
$x =floor ($this->width/$this->codenum) * $i +5;
$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, $this->height-8);
Imagettftext ($this->image, $fontsize, Rand ( -45,45), $x, $y, $fontcolor, $fontFace, $this->checkcode{$i});
}
}
}

Private Function Createcheckcode () {
$code = "23456789abcdefghijkmnpqrstuvwrst";
$str = "";
For ($i =0 $i < $this->codenum; $i + +)
{
$char = $code {rand (0,strlen ($code)-1)};
$str. = $char;
}
return $str;
}
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 picture verification Code");
}
}
Output an image to the browser by this method
function ShowImage ($fontFace = "")
{
Create an image background
$this->createimage ();
Set interference elements
$this->setdisturbcolor ();
Draw text randomly into an image
$this->outputtext ($fontFace);
Output image
$this->outputimage ();
}

function __destruct ()
{
Imagedestroy ($this->image);
}
}
function Checklogin () {
if (Empty ($_post[' name '))
Die (' username cannot be empty ');
if (Empty ($_post[' password '))
Die ("Password cannot be blank");
if ($_session[' code ']!=$_post[' vertify '])
Die ("Validation code input is incorrect". $_session[' Code '));

$username =$_post[' name '];
$password =md5 ($_post[' password '));
Check to see if there is
Conndb ($username, $password);
}
function conndb ($name = "", $ps = "") {
$conn =mysql_connect (' localhost ', ' root ', ' 123456 ');
if (! $conn) Die ("Database connection Failed". Mysql_error ());
mysql_select_db (' 5kan ', $conn) or Die (' Select Database Failed '. Mysql_error ());
Mysql_set_charset (' UTF8 ', $conn);
$sql = "SELECT id from K_user where username= ' {$name} ' and password= ' {$ps} '";
$result =mysql_query ($sql) or Die ("SQL statement error". Mysql_error ());
if (mysql_num_rows ($result) >0) die ("Login succeeded");
Else die ("Username or password error");
Mysql_close ($conn);
}
Session_Start ();
if (!isset ($_post[' randnum '))
{
$code =new Validationcode (120,20,4);
$code->showimage ("Comicbd.ttf"); Show on page
$_session[' code ']= $code->getcheckcode ();//Save in Server
}
Else
{
Checklogin ();
}

?>


To the specific call place, in such a form: on it, validation of the session:$_session[' Vcode ' value can be. You can also modify the above code slightly, and change it to two numbers to add and sum the form

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.