Share a classic PHP Verification Code Class

Source: Internet
Author: User
This article mainly introduces a classic PHP Verification Code class sharing. This article not only provides class code, but also provides examples and methods used in the form. For more information, see

This article mainly introduces a classic PHP Verification Code class sharing. This article not only provides class code, but also provides examples and methods used in the form. For more information, see

We use the GD library image processing content of PHP to design a verification code Vcode. Declare this class in the file vcode. class. php, and encapsulate some implementation details in this class through the object-oriented feature. When creating an object, you can create a verification code object by providing three parameters for the constructor, including the width, height, and number of letters in the verification code image. The declaration code for this class is as follows:

In the above script, although there are many Vcode statements for the verification code class, the details are encapsulated in the class. As long as the object is output directly, the image can be output to the client browser, it can also be used in browser forms. In addition, this class automatically obtains the character strings in the verification code image and facilitates them in the $ _ SESSION ["code"] of the service. When submitting a form, the form can be submitted successfully only when the user enters the text displayed on the Verification Code image in the form and completely matches the verification code string retained on the server. (Note: The verification code is on the server side in $ _ SESSION ["code"], so you must enable session to use this class ,)

In the following script code. php, use session_start () to enable user session control, and then include the Vcode. class. php file of the Verification code class. This class object is created and output directly. The randomly generated Verification Code image can be sent out, and the verification code string is automatically saved on the server. The Code is as follows:

 Width = $ width; $ this-> height = $ height; $ this-> num = $ num; $ this-> code = $ this-> createcode (); // call your own method} // obtain the character verification code, used to save the function getcode () {return $ this-> code in the server ;} // output image function outimg () {// create a background (color, size, border) $ this-> createback (); // draw a text (size, font color) $ this-> outstring (); // interference element (point, line) $ this-> setdisturbcolor (); // output image $ this-> printimg ();} // create a background private function createback () {// create a resource $ this-> img = imagecreatetruecolor ($ this-> width, $ this-> height ); // set the random background color $ bgcolor = imagecolorallocate ($ this-> img, rand (225,255), rand (225,255), rand (225,255 )); // set the background to imagefill ($ this-> img, 0, 0, $ bgcolor); // draw the border $ bordercolor = imagecolorallocate ($ this-> img, 0, 0, 0); imagerectangle ($ this-> img, 0, 0, $ this-> width-1, $ this-> height-1, $ bordercolor );} // draw the private function outstring () {for ($ I = 0; $ I <$ this-> num; $ I ++) {$ color = imagecolorallocate ($ this-> img, rand (0,128), rand (0,128), rand (0,128); $ fontsize = rand (3, 5 ); // font size $ x = 3 + ($ 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) ;}// sets the interference element private function setdisturbcolor () {// Add the number of 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 a line for ($ I = 0; $ I <10; $ I ++) {$ color = imagecolorallocate ($ this-> img, rand (0,255 ), rand (0,128), rand (0,255); imagearc ($ this-> img, rand (-10, $ this-> width + 10), rand (-10, $ this-> height + 10), rand (30,300), 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 the verification code string private function createcode () {$ codes = "3456789 abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY "; $ code = ""; for ($ I = 0; $ I <$ this-> num; $ I ++) {$ code. = $ codes {rand (0, strlen ($ codes)-1) };}return $ code ;}// used to automatically destroy image resources function _ destruct () {imagedestroy ($ this-> img );}}

In the following script code. php, use session_start () to enable user session control, and then include the Vcode. class. php file of the Verification code class. This class object is created and output directly. The randomly generated Verification Code image can be sent out, and the verification code string is automatically saved on the server. The Code is as follows:

 Getcode (); // output the verification code image $ vcode-> outimg ();?>

The form code is as follows:

 ";} Incorrect else {echo!
";}}?>

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.