Dynamic Verification Code settings on the login page, dynamic verification code on the page

Source: Internet
Author: User

Dynamic Verification Code settings on the login page, dynamic verification code on the page
Dynamic Verification Code settings on the logon page

Use the idea of creating an object in php to set a dynamic verification code

1. Create a background image to store the dynamic code output location.
1 function createImage () {2 // create an image object and set the image width and height to imagecreatetruecolor 3 $ this-> image = imagecreatetruecolor ($ this-> width, $ this-> height); 4 // create background color for the image 5 // rand (lower bound, Upper Bound), random number 6 $ backgroundColor = imagecolorallocate ($ this-> image, rand (225,255), rand (225,255), rand (225,255); 7 // fill in the background color 8 imagefill ($ this-> image, 0, 0, $ backgroundColor ); 9 // set the border 10 for the verification code $ borderColor = imagecolorallocate ($ this-> image, 0, 0, 0 ); // black border 11 // draw border 12 imagerectangle ($ this-> image, 0, 0, $ this-> width-1, $ this-> height-1, $ borderColor); 13}
2. Set the interference information in the dynamic code to (point, line ·······)

 

Function setDisturbElement () {// Number of randomly generated interference items $ lineNumbers = rand (2, 4); // each interference line for ($ I = 0; $ I <$ lineNumbers; $ I ++) {// top left to bottom right $ x1 = rand (2, $ this-> width/2); $ y1 = rand (2, $ this-> height/2); $ x2 = rand ($ this-> width/2, $ this-> width-2 ); $ y2 = rand ($ this-> height/2, $ this-> height-2); $ color = imagecolorallocate ($ this-> image, rand (100,200 ), rand (100,200), rand (100,200); // draw line imageline ($ this-> image, $ x1, $ y1, $ x2, $ y2, $ color );}}

 

 

 

3. Generate the string information in the verification code string and store it in the local session to facilitate information verification.

 

1 private function createCheckingCode () {2 $ code = "0123456789 qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDASZXCVBNM"; 3 // echo $ code; 4 // string receiving the Verification code 5 $ string = ''; 6 for ($ I = 0; $ I <$ this-> codeNumbers; $ I ++) {7 $ index = rand (0, (strlen ($ code) -1); 8 // echo $ index; 9 $ string. = substr ($ code, $ index, 1); // obtain the character whose subscript of the code String is $ index and add it to the string String 10} 11 return $ string; 12} 13 // set the verification code text for the image 14 function setValidationCode () {15 for ($ I = 0; $ I <$ this-> codeNumbers; $ I ++) {16 $ x = rand (1, 4) + $ this-> width * $ I/$ this-> codeNumbers; 17 $ y = rand (1, $ this-> height/4); 18 $ color = imagecolorallocate ($ this-> image, rand (0,128), rand (0,128), rand (0,128 )); 19 $ fontSize = rand (6, 8); 20 // draw text 21 imagestring ($ this-> image, $ fontSize, $ x, $ y, $ this-> checkingCode [$ I], $ color); 22} 23} 24 // store the verification code to the session 25 function saveVcodeToSession () {26 include_once ('session. php '); 27 $ session = new Session (); 28 $ session-> set ('checkingcode', $ this-> checkingCode, 10); 29}

 

 

 

 

 

4. Perform frontend output on the generated Verification Code

 

1 function outputImage () {2 $ this-> createImage (); // draw an image 3 $ this-> setDisturbElement (); // draw a line 4 $ this-> setValidationCode (); // generate a text 5 header ("Content-Type: image/png"); // set the document output Type, png type | jpg6 imagepng ($ this-> image); 7}

 

 

Complete code

 

1 <? Php 2 // Save the verification code value in the session to facilitate the front-end comparison and verification 3 session_start (); 4/** 5 * Verification code generation class 6 */7 class Validationcode 8 {9 // private, private member, limited to internal use of 10 private $ width; 11 private $ height; 12 private $ codeNumbers; // The number of verification codes 13 private $ image; // The image 14 private $ checkingCode to be output in the future; // The verification code string, 15 function _ construct ($ width = 80, $ height = 30, $ codeNumbers = 4) will be saved in the session array // if no parameter is provided, 80, 30, 4 is the default value 16 {17 $ this-> width = $ Width; 18 $ this-> height = $ height; 19 $ this-> codeNumbers = $ codeNumbers; 20 $ this-> checkingCode = $ this-> createCheckingCode (); // generate a Random verification code 21 // echo $ this-> checkingCode; 22} 23 // create a background image function 24 function createImage () {25 // create an image object, and set the image width and height to imagecreatetruecolor26 $ this-> image = imagecreatetruecolor ($ this-> width, $ this-> height ); 27 // create background color 28 // rand (lower bound, Upper Bound), random number 29 $ backgroundColor = imagecoloralloca in php Te ($ this-> image, rand (225,255), rand (225,255), rand (225,255); 30 // fill in the background color 31 imagefill ($ this-> image, 0, 0, $ backgroundColor); 32 // set the border for the verification code 33 $ borderColor = imagecolorallocate ($ this-> image, 0, 0, 0 ); // black border 34 // draw border 35 imagerectangle ($ this-> image, 0, 0, $ this-> width-1, $ this-> height-1, $ borderColor); 36} 37 // set interference item 38 function setDisturbElement () {39 // Number of randomly generated interference Items 40 $ lineNumbers = rand (2, 4 ); 41 // generate each interference line through the for loop 42 for ($ I = 0; $ I <$ lineNumbers; $ I ++) {43 // from the upper left corner to the lower right corner 44 $ x1 = rand (2, $ this-> width/2); 45 $ y1 = rand (2, $ this-> height/2); 46 $ x2 = rand ($ this-> width/2, $ this-> width-2 ); 47 $ y2 = rand ($ this-> height/2, $ this-> height-2); 48 $ color = imagecolorallocate ($ this-> image, rand (100,200 ), rand (100,200), rand (100,200); 49 // draw line 50 imageline ($ this-> image, $ x1, $ y1, $ x2, $ y2, $ color); 51} 52} 53 // output the created Verification Code image 54 function outputImage () {55 $ this-> createImage (); // draw an image 56 $ this-> setDisturbElement (); // draw a line 57 $ this-> setValidationCode (); // generate a text 58 header ("Content-Type: image/png "); // set the document output type, png type | jpg59 imagepng ($ this-> image ); 60} 61 // generate verification code string 62 // private function 63 private function createCheckingCode () {64 $ code = "0123456789 qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDASZXCVBNM"; 65/ec Ho $ code; 66 // string for receiving the verification code 67 $ string = ''; 68 for ($ I = 0; $ I <$ this-> codeNumbers; $ I ++) {69 $ index = rand (0, (strlen ($ code)-1); 70 // echo $ index; 71 $ string. = substr ($ code, $ index, 1); // obtain the character whose subscript is $ index and add it to String 72} 73 return $ string; 74} 75 // set the verification code text for the image 76 function setValidationCode () {77 for ($ I = 0; $ I <$ this-> codeNumbers; $ I ++) {78 $ x = rand (1, 4) + $ this-> width * $ I/$ this-> codeNumb Ers; 79 $ y = rand (1, $ this-> height/4); 80 $ color = imagecolorallocate ($ this-> image, rand (0,128 ), rand (0,128), rand (0,128); 81 $ fontSize = rand (6, 8); 82 // draw text 83 imagestring ($ this-> image, $ fontSize, $ x, $ y, $ this-> checkingCode [$ I], $ color); 84} 85} 86 // store the verification code to the session 87 function saveVcodeToSession () {88 include_once ('session. php '); 89 $ session = new Session (); 90 $ session-> set ('checkingcode', $ this-> ch EckingCode, 10); 91} 92} 93 $ vcode = new Validationcode (); 94 $ vcode-> outputImage (); 95 $ vcode-> saveVcodeToSession (); 96?>
Php

 

 

 

 

 

 

 

 

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.