John elaborated on the PHP verification code, and John elaborated on the PHP verification code.

Source: Internet
Author: User
Tags imagejpeg

John elaborated on the PHP verification code, and John elaborated on the PHP verification code.

Let's talk about how to create a verification code class in php.

I wrote a verification code class here. I will demonstrate how to use it. I am a cainiao. Please skip it. Let me explain how to use it. It takes two steps in total.

Step 1:

Download the created verification code. : Http://files.cnblogs.com/files/xfjpeter/Verify.zip

Step 2:

1. Create a verification code file for one word

1 <? Php 2 3 # introduce the verification code file 4 require_once ('verify. class. php '); 5 6 # instantiate the Verification Code Class 7 # Four parameters can be passed for initialization: the length, height, and length of the verification code, type of the Verification code (the bgRand attribute must be set to false for the verification code type) 8 $ code = new Verify (140, 40, 6, 6 ); 9 10 # Set the length of the Verification code image 11 $ code-> width = 200; 12 13 # Set the height of the Verification code image 14 $ code-> height = 60; 15 16 # Whether the background is random. The default value is true (random) 17 $ code-> bgRand = false; 18 19 # display the Verification code 20 $ code-> verify ();

The generated image style is

 

2. The verification code file is

1 <? Php 2 3/** 4 * Verification Code class 5 * @ author John <fsyzxz@163.com> 6 */7 class Verify 8 {9 private $ width = 160; // Verification Code width 10 private $ height = 60; // Verification Code height 11 private $ type = 1; // verification code type 12 private $ length = 4; // The verification code length is 13 private $ code; // Verification code 14 private $ img; // image resource 15 private $ seKey = 'john '; // KEY 16 private $ bgRand = true; // random background image 17 18/** 19 * constructor 20 * @ param type $ width Verification Code width 21 * @ param type $ heig Ht Verification Code height 22 * @ param type $ length verification code length 23 * @ param type $ type verification code type 24 */25 public function _ construct ($ width = 160, $ height = 40, $ length = 4, $ type = 1) 26 {27 $ this-> width =! Empty ($ width )? $ Width: $ this-> width; 28 $ this-> height =! Empty ($ height )? $ Height: $ this-> height; 29 $ this-> length =! Empty ($ length )? $ Length: $ this-> length; 30 $ this-> type =! Empty ($ type )? $ Type: $ this-> type; 31} 32 33/** 34 * set the attribute value 35 * @ param type $ name attribute name 36 * @ param type $ value Attribute value 37 */38 public function _ set ($ name, $ value) 39 {40 if (isset ($ name) {41 $ this-> $ name = $ value; 42} 43} 44 45/** 46 * get attribute value 47 * @ param type $ name attribute name 48 * @ return type return attribute value 49 */50 public function _ get ($ name) {51 return $ this-> $ name; 52} 53 54/** 55 * Verification code 56 * @ param type $ code form Verification code 57 * @ return boolean 58 */59 public function check ($ code) {60 if (! Isset ($ _ SESSION) {session_start ();} 61 if ($ this-> encodeVerify (strtolower ($ code) ===$ _ SESSION ['code']) {62 return true; 63} else {64 return false; 65} 66} 67 68 // output verification code 69 public function verify () 70 {71 $ this-> code = $ this-> createVerify (); 72 // create a background 73 $ this-> createBackground (); 74 // text display 75 $ this-> writeString (); 76 // draw interference line 77 $ this-> paitLine (); 78 // input image 79 $ this-> printImg (); 80} 81 82 /** 83 * Create a background image 84 */85 private function createBackground () 86 {87 // create an image from the image library and determine if it is random 88 if ($ this-> bgRand) {89 $ img = imagecreatefromjpeg ('. /verify/bgs/'.mt_rand(1,82.16.'.jpg '); 90} else {91 $ img = imagecreatefromjpeg ('. /verify/bgs /'. $ this-> type.'.jpg '); 92} 93 // create an image 94 $ this-> img = imagecreatetruecolor ($ this-> width, $ this-> height ); 95 // copy the image to the created image 96 imagecopyresampled ($ this-> img, $ img, 0, 0, 0, 0, $ this-> width, $ this-> height, imagesx ($ img), imagesy ($ img )); 97} 98 99/** 100 * write 101 */102 private function writeString () 103 {104 $ color = imagecolorallocatealpha ($ this-> img, mt_rand (0,128), mt_rand (0,128), mt_rand (0,128), 0); 105 $ fontType = '. /verify/ttfs /'. mt_rand (1, 6 ). '. ttf'; 106 $ fontSize = mt_rand (15, 20); 107 for ($ I = 0; $ I <$ this-> length; $ I ++) {108 $ x = 3 + ($ this-> width/$ this-> Length) * $ I; 109 $ y = mt_rand ($ this-> height/3) * 2, ($ this-> height/3) * 2 ); 110 // write the verification code on the image 111 imagettftext ($ this-> img, $ fontSize, 0, $ x, $ y, $ color, $ fontType, $ this-> code [$ I]); 112} 113} 114 115/** 116 * draw interference lines and letters 117 */118 private function paitLine () 119 {120 $ px = $ py = 0; 121 $ codes = '2345678abcdefhijkmnpqrstuvwxyz '; 122 for ($ I = 0; $ I <$ this-> width/4; $ I ++) {123 $ num = mt_rand (0, strlen ($ codes)-1); 124 $ Color = imagecolorallocatealpha ($ this-> img, 255,255,255, 80); 125 // draw the letter 126 imagechar ($ this-> img, 8, mt_rand (3, $ this-> width), mt_rand (3, $ this-> height), $ codes {$ num}, $ color ); 127} 128} 129 130/** 131 * input image 132 */133 private function printImg () 134 {135 if (function_exists ('imagegif ')) {136 // for gif Response header ('content-Type: image/gif'); 138 imagegif ($ this-> img); 139} elseif (function_exists ('imagejpeg ') {140 // For the response 141 header ('content-Type: image/jpeg'); 142 imagejpeg ($ this-> img, NULL, 100 ); 143} elseif (function_exists ('imagepng ') {144 // For PNG145 header ('content-Type: image/png'); 146 imagepng ($ this-> img ); 147} elseif (function_exists ('imagewbmp ') {148 // For wbmp 149 header ('content-Type: image/vnd. wap. wbmp '); 150 imagewbmp ($ this-> img); 151} 152 153 154/** 155 * generate Verification Code 156 * @ return string return the generated Verification Code Code 157 */158 private function createVerify () 159 {160 $ codeSet = '2345678abcdefhijkmnpqrstuvwxyz '; 161 $ codes = ''; 162 for ($ I = 0; $ I <$ this-> length; $ I ++) {163 $ codes. = $ codeSet [mt_rand (0, strlen ($ codeSet)-1)]; 164} 165 // Save the verification code to the session 166 if (! Isset ($ _ SESSION) {session_start ();} 167 $ _ SESSION ['code'] = $ this-> encodeVerify (strtolower ($ codes )); 168 // $ _ SESSION ['code'] = $ codes; 169 return $ codes; 170} 171 172/** 173 * encryption Verification Code 174 * @ param type $ string175 * @ return type176 */177 private function encodeVerify ($ string) 178 {179 $ key = substr (md5 ($ this-> seKey), 5, 8); 180 $ str = substr (md5 ($ string), 8, 10 ); 181 return md5 ($ key. $ str); 182} 183 184/** 185 * destroy image 186 */187 function _ destruct () 188 {189 if (isset ($ this-> img )) {190 imagedestroy ($ this-> img); 191} 192} 193}

The two steps above give birth to the verification you want.

In addition, Verify. class. php has a method to Verify whether the verification code is correct.

Pass the verification code you obtained from the interface to the code method.

If ($ code-> code (this is the verification code value obtained on your page) {# This is the correct verification operation} else {# Verification Failed operation}

The above is my experience in creating the entire verification code. I hope it will help those who click here.

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.