Php generates an image verification code-five verification codes are attached

Source: Internet
Author: User
This article mainly explains how to generate an image verification code in php, including a digital verification code, a digital and letter verification code, a Chinese verification code, a google-like verification code, and an arithmetic verification code, generate a tape

This article mainly explains how to generate an image verification code in php, including a digital verification code, a digital and letter verification code, a Chinese verification code, a google-like verification code, and an arithmetic verification code, generate a tape

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 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! ");}});});

// 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.

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.