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
2.3 define a check () function