PHP verification code generation and verification example _ PHP Tutorial

Source: Internet
Author: User
PHP verification code generation and verification example. Verification code is a common feature in Web. for example, the registration code is required to verify the legality of the operator, we can see that some Web verification codes are a common feature in web, such as registration, logon, or message pages. the registration code is required to verify the legality of the current operator, we will see that some websites do not have verification codes, but they are more advanced verification. next we will look at the common verification code generation and use methods.

1. a simple verification code instance

1.1 display the verification code picture

The code is as follows:

$ Num = intval (mt_rand (1000,9999 ));
For ($ I = 0; $ I <4; $ I ++)
{Echo "";}
?>

1.2 verification process

The code is as follows:

If (strval ($ inputyzm )! = Strval ($ num ))
{
Echo "script" alert ('verification code is incorrect! '); History. go (-1); script ";
Exit;
}

2. example of a Chinese character verification code

2.1 display the verification code picture

The code is as follows:
$ Str = "Han", "word", "verification", "certificate", "code"); // you can define the content and number of Chinese characters.
$ Word = strlen ($ str ));
For ($ I = 0; $ I <4; $ I ++)
{
$ Num = rand (0, $ word );
$ Img = $ img ."";
$ Pic = $ pic. $ str [$ num];
}
>

2.2 assign the generated random string to a hidden field

The code is as follows:

2.3 define a check () function

The code is as follows:

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.