Php verification code generation program code

Source: Internet
Author: User

This article describes how to use session storage and the gd library to generate a verification code program, and add some interference elements to prevent machine registration, next I will introduce you to you.

PHP verification code and the image program are generated. session recognition is used to slightly improve the PHP Verification Code circulating on the network. The mixed points are added, and the numbers are displayed randomly to control the display of four digits; the program is as follows.

Create a new yz. php verification code file:

Note: The following code needs to open the GD library of php, modify the configuration of the php. in file, and cancel the semicolon before the commented-out line: extension = php_gd2.dll.

The Code is as follows: Copy code

<? Php
Class ValidationCode
{
// Attributes
Private $ width;
Private $ height;
Private $ codeNum;
Private $ image;
Private $ disturbColorNum; // Number of interference 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 );
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), $ 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 = "23456789 abcdefghijkmnpqrstuvwrst ";
$ 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 image Verification Code ");
}
}
// Output the image to the browser using this method
Function showImage ($ fontFace = "")
{
// Create an image background
$ This-> createImage ();
// Sets the interference element.
$ This-> setDisturbColor ();
// Randomly draw text from the image
$ This-> outputText ($ fontFace );
// Output image
$ This-> outputImage ();
}

Function _ destruct ()
{
Imagedestroy ($ this-> image );
}
}
Function checklogin (){
If (empty ($ _ POST ['name'])
Die ('user name cannot be blank ');
If (empty ($ _ POST ['Password'])
Die ("password cannot be blank ");
If ($ _ SESSION ['code']! = $ _ POST ['version'])
Die ("Incorrect verification code input". $ _ SESSION ['code']);

$ Username = $ _ POST ['name'];
$ Password = md5 ($ _ POST ['Password']);
// Check for existence
Conndb ($ username, $ password );
}
Function conndb ($ name = "", $ ps = ""){
$ Conn = mysql_connect ('localhost', 'root', '123 ');
If (! $ Conn) die ("database connection failed". mysql_error ());
Mysql_select_db ('5kan ', $ conn) or die ('database selection 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 ("Logon successful ");
Else die ("incorrect user name or password ");
Mysql_close ($ conn );
}
Session_start ();
If (! Isset ($ _ POST ['randnum'])
{
$ Code = new ValidationCode (120,20, 4 );
$ Code-> showImage ("comicbd. ttf"); // displayed on the page
$ _ SESSION ['code'] = $ code-> getCheckCode (); // save it to the server
}
Else
{
Checklogin ();
}

?>


To the specific place of the call, the form is as follows: . You can verify the value of session: $ _ SESSION ['vcode'] During verification. You can also slightly improve the above Code by adding and summation two numbers.

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.