PHP generated picture verification code-with five kinds of verification code _php instance

Source: Internet
Author: User
Tags arithmetic rand

Before the output verification code used a method, in the foreground with JS to generate a code string, and then passed to the background with PHP output CAPTCHA code image. This does not require the use of $_session to pass the verification code value, directly using JS to compare the generated string and the input string is equal.

This article demonstrates 5 kinds of authentication codes with an example, and introduces the function of generating the verification code. PHP generated Verification code principle: Through the GD library, generate a picture with the verification code, and the verification code stored in the session.

1, HTML

The verification code in 5 has the following HTML code:

<div class= "Demo" >


  
 
 <H3>2, Digital + letter verification Code  
  
 <H3>3, Chinese authentication 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 ("Verify code is correct!) ");
  } else {
  alert ("Authentication code 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 (" Verify the code is correct! ");
  } else {
  alert ("Authentication 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 ("Verify code is correct!) ");
  } else {
  alert ("Authentication 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 (" Verify the code is correct! ");
  } else {
  alert ("Authentication 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 (" Verify the code is correct! ");
  } else {
  alert ("Authentication code 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);
 The///4-bit captcha can also be generated directly by rand (1000,9999)//To write the generated validation code into session, with $_session["helloweba_num" = $code;
 Create a picture that defines 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 a border Imagerectangle ($im, 0, 0, $w-1, $h-1, $black);
 Randomly draw two dotted lines, interfering $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 spots are randomly generated on the canvas, which plays a disturbing role.
 for ($i = 0; $i < $i + +) {Imagesetpixel ($im, rand (0, $w), rand (0, $h), $black); ///Display the numbers randomly on the canvas, the horizontal spacing and position of the characters are randomly generated according to 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 Picture Imagedestroy ($im);//Free up memory for pictures}

The above content is PHP to generate picture verification code-with five kinds of verification code of the entire content, I hope you like.

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.