Example of generating an image verification code using php ,. An example of generating an image verification code using php is described. This article uses an example to demonstrate five verification codes and describes the functions used to generate verification codes. Principles of generating a verification code using PHP: an example of generating an image verification code with a verification code using the GD Library is described,
This article uses examples to demonstrate five verification codes and describes the functions used to generate verification codes. How PHP generates a verification code: generate an image with the verification code through the GD library, and save the verification code in the Session.
1. HTML
5. the HTML code of the verification code is as follows:
1. digital verification code
Verification code:
2. digit + letter verification code
Verification code:
3. Chinese verification code
Verification code:
4. google verification code imitation
Verification code:
5. arithmetic verification code
Verification code:
2. js verification
$ (Function () {$ ("# getcode_num "). click (function () {// number verification $ (this ). attr ("src", 'Code _ num. php? '+ Math. random () ;}); $ ("# chk_num "). click (function () {var code_num = $ ("# code_num "). val (); $. post ("chk_code.php? Act = num ", {code: code_num}, function (msg) {if (msg = 1) {alert (" The verification code is correct! ");} Else {alert (" verification code error! ") ;}}) ;}); // Number + letter verification $ (" # getcode_char "). click (function () {$ (this ). attr ("src", 'Code _ char. php? '+ Math. random () ;}); $ ("# chk_char "). click (function () {var code_char = $ ("# code_char "). val (); $. post ("chk_code.php? Act = char ", {code: code_char}, function (msg) {if (msg = 1) {alert (" The verification code is correct! ");} Else {alert (" verification code error! ") ;}}) ;}); // Chinese verification code $ (" # getcode_zh "). click (function () {$ (this ). attr ("src", 'Code _ zh. php? '+ Math. random () ;}); $ ("# chk_zh "). click (function () {var code_zh = escape ($ ("# code_zh "). val (); $. post ("chk_code.php? Act = zh ", {code: code_zh}, function (msg) {if (msg = 1) {alert (" The verification code is correct! ");} Else {alert (" verification code error! ") ;}}) ;}); // Google verify $ (" # getcode_gg "). click (function () {$ (this ). attr ("src", 'Code _ gg. php? '+ Math. random () ;}); $ ("# chk_gg "). click (function () {var code_gg = $ ("# code_gg "). val (); $. post ("chk_code.php? Act = gg ", {code: code_gg}, function (msg) {if (msg = 1) {alert (" The verification code is correct! ");} Else {alert (" verification code error! ") ;}}) ;}); // Arithmetic verification $ (" # getcode_math "). click (function () {$ (this ). attr ("src", 'Code _ math. php? '+ Math. random () ;}); $ ("# chk_math "). click (function () {var code_math = $ ("# code_math "). val (); $. post ("chk_code.php? Act = math ", {code: code_math}, function (msg) {if (msg = 1) {alert (" The verification code is correct! ");} Else {alert (" verification code error! ");}});});});
3. PHP generates a verification code
Session_start (); getCode (4,60, 20); function getCode ($ num, $ w, $ h) {$ code = ""; for ($ I = 0; $ I <$ num; $ I ++) {$ code. = rand (0, 9);} // A four-digit verification code can also be directly generated using rand (,) // The generated verification code is written into the session, during backup verification, use $ _ SESSION ["helloweba_num"] = $ code; // create an image and define the color value header ("Content-type: image/PNG "); $ im = imagecreate ($ w, $ h); $ black = imagecolorallocate ($ im, 0, 0, 0); $ gray = imagecolorallocate ($ im, 200,200,200 ); $ bgcolor = imagecolorallocate ($ im, 255,255,255); // fill in the background imagefill ($ im, 0, 0, $ gray); // draw the border imagerectangle ($ im, 0, 0, $ W-1, $ H-1, $ black); // randomly draw two dotted lines to interfere with $ style = array ($ black, $ black, $ black, $ gray, $ gray); imagesetstyle ($ im, $ style); $ y1 = rand (0, $ h ); $ y2 = rand (0, $ h); $ y3 = rand (0, $ h); $ y4 = rand (0, $ h); imageline ($ im, 0, $ y1, $ w, $ y3, IMG_COLOR_STYLED); imageline ($ im, 0, $ y2, $ w, $ y4, IMG_COLOR_STYLED ); // randomly generate a large number of black spots on the canvas, which can interfere. for ($ I = 0; $ I <80; $ I ++) {imagesetpixel ($ im, rand (0, $ w), rand (0, $ h), $ black);} // randomly display numbers on the canvas, the horizontal spacing and position of characters are randomly generated according to a certain fluctuation range $ strx = rand (3, 8); for ($ I = 0; $ I <$ num; $ I ++) {$ strpos = rand (1, 6); imagestring ($ im, 5, $ strx, $ strpos, substr ($ code, $ I, 1 ), $ black); $ strx + = rand (8, 12);} imagepng ($ im); // output image imagedestroy ($ im); // release the memory occupied by the image}
The above is all the content of this article, hoping to help you learn.
Token. This article uses an example to demonstrate five verification codes and describes the functions used to generate verification codes. How PHP generates a verification code: generate a verification code with the GD library...