Php generates image verification codes-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:
<Div class = "demo">
<H3> 2. digit + letter Verification Code
<H3> 3. Chinese Verification Code
<H3> 4. google-like Verification Code
<H3> 5. Arithmetic 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! ");}});});
// 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.