PHP generated image verification code-with five kinds of verification code, _php tutorial

Source: Internet
Author: User

PHP generated image verification code-with five kinds of verification code,


Previous output verification code used a method, in the foreground with JS generated verification code string, and then passed to the background with PHP output verification code image. In this way, you do not need to use $_session to pass the verification code value, directly with the JS comparison between the generated string and the input string is equal.

In this paper, we demonstrate 5 kinds of verification codes, and describe the function of generating verification code. PHP generated Verification code principle: Through the GD library, generate a picture with a verification code, and save the Verification code in the session.

1. HTML

5 The code is as follows:

1. Digital Verification Code

Verification Code:

2, Digital + Letter verification Code

Verification Code:


3. Chinese Verification Code

Verification Code:


4, imitation Google verification code

Verification Code:


5. Arithmetic Verification Code

Verification Code:

2. JS Verification

$ (function () {$ ("#getcode_num"). Click (function () {//Digital authentication $ (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 CAPTCHA is correct!") ");  } else {  alert ("CAPTCHA Error! ");  } }); });

Digital + 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 CAPTCHA is correct!") ");  } else {  alert ("CAPTCHA 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 CAPTCHA is correct!") ");  } else {  alert ("CAPTCHA Error! ");  } }); });

Google authentication

$ ("#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 CAPTCHA is correct!") ");  } else {  alert ("CAPTCHA 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 CAPTCHA is correct!") ");  } else {  alert ("CAPTCHA Error! ");  } }); });});

3, PHP generated verification code

Session_Start (); GetCode (4,60,20); function GetCode ($num, $w, $h) {$code = ""; for ($i = 0; $i < $num; $i + +) {$code. = rand (0, 9);}//4-bit verification code can also be generated directly with Rand (1000,9999)//To write the generated verification code to the session, for verification with $_sessio n["Helloweba_num"] = $code; Create a picture, 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 Background imagefill ($im, 0, 0, $gray); Draw Border Imagerectangle ($im, 0, 0, $w-1, $h-1, $black); Randomly draws two dashed lines, acts as a disturbance $style = Array ($black, $black, $black, $black, $black, $gray, $gray, $gray, $gray, $gray); Imagesetstyle ($im, $style); $y 1 = rand (0, $h); $y 2 = rand (0, $h); $y 3 = rand (0, $h); $y 4 = rand (0, $h); Imageline ($im, 0, $y 1, $w, $y 3, img_color_styled); Imageline ($im, 0, $y 2, $w, $y 4, img_color_styled); A large number of black dots are randomly generated on the canvas, which acts as interference; for ($i = 0; $i <, $i + +) {Imagesetpixel ($im, rand (0, $w), rand (0, $h), $black), and//numbers are randomly displayed on the canvas, and the horizontal spacing and position of the characters are in accordance with the range of fluctuations Machine Generation $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 Picture Imagedestroy ($im);//release Image Memory}

The above content is PHP generated image verification code-with five kinds of verification code of all content, I hope you like.

http://www.bkjia.com/PHPjc/1049140.html www.bkjia.com true http://www.bkjia.com/PHPjc/1049140.html techarticle PHP generated image verification code-with five kinds of verification code, the previous output verification code used a method, in the foreground with JS generated verification code string, and then passed to the background with PHP output verification code image ...

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