Php generates an image verification code-five verification codes are attached ,. Php generates an image verification code-five verification codes are attached. a verification code string is generated using JS on the front-end when the verification code is output, pass the verification code to the background and use PHP to output the verification code Image. php generates the image verification code.-Five verification codes are attached,
I used to output the verification code in the past. I used JavaScript to generate a verification code string at the front end, and then passed it to the backend to output the verification code image using PHP. In this way, you do not need to use $ _ SESSION to pass the verification code value during verification. you can directly use JS to compare the generated string with the input string to see if it is equal.
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 codeVerification code:
3. Chinese verification codeVerification code:
4. google verification code imitationVerification code:
5. arithmetic verification codeVerification 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! ");}});});
// Digit + 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 verification
$ ("# 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 content is the php image verification code generated-with all the content of the five verification codes, I hope you will like it.
Token, I used a method to output the verification code before. on the front end, I used JS to generate the verification code string, and then passed it to the background to output the verification code image using PHP...