A classic PHP verification code class sharing _ PHP

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 forms, you can refer to the image processing content in the GD library 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:

<? Php class Vcode {private $ width; // width private $ height; // high private $ num; // quantity private $ code; // verification code private $ img; // image resource // Constructor. 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} // Obtain the character verification code, which is saved in the server function getcode () {return $ this-> code;} // output image function outimg () {// create a background (color, size, border) $ this-> createback (); // draw (size, font color) $ this-> outstring (); // interfere with elements (points, lines) $ this-> setdisturbcolor (); // output image $ this-> printimg ();} // create the 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 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:

<? Php // enable session session_start (); include "vcode. class. php "; // Constructor $ vcode = new Vcode (80, 30, 4 ); // put the verification code in the server's own space and save it $ _ SESSION ['code'] = $ vcode-> getcode (); // output the verification code Image $ vcode-> outimg ();?>

The form code is as follows:

<? Php session_start (); if (isset ($ _ POST ['dosubmit ']) {if (strtoupper ($ _ SESSION ['code']) = strtoupper ($ _ POST ['code']) {echo "input successful!
";} Incorrect else {echo!
";}}?>

Download PHP classic verification code:

PHP verification code class .rar

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.