Verification Code class and usage in PHP

Source: Internet
Author: User

 

Note: add the public page to session_start ();

 

<? Php

Session_start ();

Ini_set ('display _ errors ', 'off ');

Class CCheckCodeFile

{

// Number of digits of the Verification Code www.2cto.com

 

Var $ mCheckCodeNum = 4;

 

// Generated Verification Code

Var $ mCheckCode = '';

 

// Verification Code Image

Var $ mCheckImage = '';

 

// Interference Pixel

Var $ mDisturbColor = '';

 

// The image width of the Verification Code

Var $ mCheckImageWidth = '80 ';

 

// The image width of the Verification Code

Var $ mCheckImageHeight = '20 ';

 

// Output Header

Function OutFileHeader ()

{

Header ("Content-type: image/png ");

}

 

// Generate a verification code

Function CreateCheckCode ()

{

// $ This-> mCheckCode = strtoupper (substr (md5 (rand (), 0, $ this-> mCheckCodeNum ));

$ This-> mCheckCode = strtoupper (substr (rand (0,999999999999), 0, $ this-> mCheckCodeNum ));

Session_cache_expire (60 );

Session_start ();

$ _ SESSION ["code_str"] = $ this-> mCheckCode;

Return $ this-> mCheckCode;

}

 

// Generate a verification code Image

Function CreateImage ()

{

$ This-> mCheckImage = @ imagecreate ($ this-> mCheckImageWidth, $ this-> mCheckImageHeight );

Imagecolorallocate ($ this-> mCheckImage, 255,255,255 );

Return $ this-> mCheckImage;

}

 

// Sets the interference pixels of the image.

Function SetDisturbColor ()

{

For ($ I = 0; $ I <= 128; $ I ++)

{

$ This-> mDisturbColor = imagecolorallocate ($ this-> mCheckImage, rand (0,255), rand (0,255), rand (0,255 ));

Imagesetpixel ($ this-> mCheckImage, rand (1,100), rand (1,100), $ this-> mDisturbColor );

}

}

 

// Set the size, width, and height of the Verification Code image.

Function SetCheckImageWH ($ width, $ height)

{

If ($ width = ''| $ height ='') return false;

$ This-> mCheckImageWidth = $ width;

$ This-> mCheckImageHeight = $ height;

Return true;

}

 

// Upload the verification code one by one on the Verification Code Image

Function WriteCheckCodeToImage ()

{

For ($ I = 0; $ I <= $ this-> mCheckCodeNum; $ I ++)

{

$ Bg_color = imagecolorallocate ($ this-> mCheckImage, rand (0,255), rand (0,255), rand (0,255 ));

$ X = floor ($ this-> mCheckImageWidth/$ this-> mCheckCodeNum) * $ I;

$ Y = rand (0, $ this-> mCheckImageHeight-15 );

Imagechar ($ this-> mCheckImage, 5, $ x, $ y, $ this-> mCheckCode [$ I], $ bg_color );

}

}

 

// Output the verification code Image

Function OutCheckImage ()

{

$ This-> OutFileHeader ();

$ This-> CreateCheckCode ();

$ This-> CreateImage ();

$ This-> SetDisturbColor ();

$ This-> WriteCheckCodeToImage ();

Imagepng ($ this-> mCheckImage );

Imagedestroy ($ this-> mCheckImage );

}

}

 

$ C_check_code_image = new CCheckCodeFile ();

// $ C_check_code_image-> SetCheckImageWH (, 50); // you can specify the image size of the verification code.

$ C_check_code_image-> OutCheckImage ();

?>

 

 

 

Usage:

<Tr>

<Td align = "center" valign = "middle"> Verification Code </td>

<Td align = "center" valign = "middle"> <input name = "number" type = "text" style = "width: 80px;">

</Td>

</Tr>

 

 

Verify if it is correct

 

Session_start ();

 

If ($ _ POST ["number"] ==$ _ SESSION ["code_str"]) {

// Echo ("the verification code is correct ");

} Else {

Echo"

 

<Script>

 

Var s = 10;

 

Function OK ()

 

{

 

S --;

 

Document. getElementById ('tids'). innerHTML = s;

 

If (s = 0)

 

{

 

ClearTimeout (t );

 

History. go (-1 );

 

}

 

}

 

Var t = setInterval ('OK ()', 1000 );

 

</Script>

 

". "Enter the verification code. Please re-enter <span id = 'tids'> </span> seconds to jump to <a href = \" javascript: history. go (-1) \ "> previous page </a> ";

}

 

From http://itsafe.org /? P = 384

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.