Php code for generating a 4-digit Verification Code

Source: Internet
Author: User
This article mainly introduces the implementation code of the php digital verification code, which has some reference value. For more information, see

This article mainly introduces the implementation code of the php digital verification code, which has some reference value. For more information, see

It is very convenient to implement the verification code in php. The key point is to master the usage of the php gd library and session.

Looking at the online php verification code generation example, there is no combination of php gd library and session, and php is used to generate a random number.

PHP verification codes can be divided into many types, includingPhp image verification code, php Random verification code,AndPhp Chinese Verification CodeAnd use different verification codes based on different application scenarios.

Here is a php digital verification code for your reference.

4-digit Verification Code

/** Filename: authpage. php */session_start (); // srand (double) microtime () * 1000000); $ authnum = $ _ SESSION ['authnum']; // verify whether the user input is consistent with the verification code if (isset ($ _ POST ['authinput']) {if (strcmp ($ _ POST ['authinput'], $ _ SESSION ['authnum']) = 0) echo "Verification Successful! "; Else echo" Verification Failed! ";}/// Generate a new four-digit integer Verification Code // while ($ authnum = rand () % 10000) <1000);?>

Authimg. php

<? Php // generate the verification code image Header ("Content-type: image/PNG"); srand (double) microtime () * 1000000 ); // play the seeds that generate random numbers to facilitate the use of session_start () for the following random number generation; // Save the random number to the session $ _ SESSION ['authnum'] = ""; $ im = imagecreate (); // specify the image background size $ black = ImageColorAllocate ($ im, 0); // set three colors $ white = ImageColorAllocate ($ im, 255,255,255); $ gray = ImageColorAllocate ($ im, 200,200,200); imagefill ($ im, $ gray); // use the area filling method, set (0, 0) while ($ authnum = rand () % 1 00000) <10000); // map the four-digit integer verification code to the image $ _ SESSION ['authnum'] = $ authnum; imagestring ($ im, 5, 10, 3, $ authnum, $ black); // use the col color to draw the string s to the x and y coordinates of the image (0, 0 in the upper left corner of the image ). // If the font is 1, 2, 3, 4, or 5, use the built-in font for ($ I = 0; $ I <200; $ I ++) // Add interference pixels {$ randcolor = ImageColorallocate ($ im, rand (0,255), rand (0,255), rand (0,255); imagesetpixel ($ im, rand () % 70, rand () % 30, $ randcolor);} ImagePNG ($ im); ImageDestroy ($ im);?>

The above is the implementation code for php to generate a 4-digit verification code. I hope it will help you learn and be able to master php Verification Code more skillfully.

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.