PHP generates an example of an image verification code, _php tutorial

Source: Internet
Author: User

PHP generates an example of a picture verification code to explain,


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!   "); }  }); }); 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 CAPTCHA is correct!")   "); } else {alert ("CAPTCHA Error!   "); }  }); }); Chinese captcha $ ("#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 verified $ ("#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 validation $ ("#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 $_sessi on["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);}//random display of numbers on the canvas, the horizontal spacing and position of the characters according to a certain fluctuation range Randomly generated $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 is the whole content of this article, I hope that everyone's study has helped.

http://www.bkjia.com/PHPjc/1042683.html www.bkjia.com true http://www.bkjia.com/PHPjc/1042683.html techarticle PHP generated An example of image verification code, this article demonstrates 5 kinds of verification code, and introduces the function of generating verification code. PHP Generation Verification Code principle: Through the GD library, generate a code with verification ...

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