Use PHP built-in functions to create a simple verification code

Source: Internet
Author: User

Because these two days to learn some of the PHP built-in functions, so today with some built-in functions with arrays to make a simple random verification code effect.

For example: 2dt5 t22c ....

Analysis: First analyze the composition of the Verification Code :

1. The verification code is generated randomly by the digital 1-9, the capital letter A- z, and the lowercase letter A- z .

2. I first create an array that contains the specified range of cells. (This should be three: numbers, uppercase letters, lowercase letters).

3. I can combine these arrays into a large array

4. Randomly disturb the function. PS: actually think here to do one more step to shuffle the array randomly, feel there is no need ah! Because what we do in the back is not a random extraction?

5. Randomly extract four times in this new array. (This requires a two-step implementation, the first step: Use the Array_rand () function to randomly remove four cells from an array and return one or more keys of a random entry (multiple returned an array, like this is to return an array containing four elements) second step: Print out this number by subscript.

Some of the built-in functions used:

(1)range (): Create an array containing the specified range of cells

1 For example: 2    //Array (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ten, One,)3     foreach (range (0, a) as $number ) {4 echo $number;

(2)array_merge (): merging one or more arrays

Form:array array_merge (array $array 1 [, Array $ ...])

1 Example: 2 <? PHP 3 $array 1  = Array ("color"  =  "Red",  2,  4); 4 $array 2  = Array ("A",  "B",  "color" = "  green",  "shape"  = " Trapezoid ",  4); 5 $result  =  Array_merge ($array 1,  $array 2); 6 Print_r ($result); 7  ?>

  1   output result:   2  array   3   (  4   [ Color] = Green   5   [0] = 2   6   [1] = 4   7
      [2] = a   8   [3] = b   9   [shape] = trapezoid< /span>10   [4] = 4  11 ) 

(2)shuffle () disturb the array

Form:bool Shuffle ( array &$array ) Note; It returns a Boolean value (TRUE or False)

Note: Since the use of this random function is returned by a Boolean value, so we can not think of their own thinking I randomly scrambled to let it output to see, this practice is wrong, I used the first time also made the same mistake.

    

Explanation: This is the first time I put the wrong, I thought this can get a scrambled array, in fact, he really returned is a Boolean value, tell us whether to disrupt success.

Modified version:

1 <? PHP 2 header ("Content-type:text/html;charset=utf-8"); 3 //Create an array 4 echo "<pre>"; 5 $arr 1 = range (1,9); 6 Var_dump ($arr 1); 7 //Randomly scrambling the array 8 Shuffle ($arr 1); 9 Var_dump ($arr 1);

    

(4)Array_rand () extracts one or more random cells from an array and returns one or more keys of a random entry (multiple words are an array)

Form:mixed array_rand ( array $input [, int $num_req = 1])

 

1 Example: 2 <? PHP 3 $input  = Array ("Neo",  "Morpheus",  "Trinity",  "Cypher",  "Tank"); 4 $rand _keys  =  Array_rand ($input,  2); 5 echo  $input [$rand _keys [0]].  " \ n "; 6 echo  $input [$rand _keys [1]].  " \ n "; 7  ?>

Then we begin to implement this verification code generation process:

1 <?PHP2 header ("Content-type:text/html;charset=utf-8");3 //Create three arrays4 $arr 1=range (1,9);//Create an array of 1-95 $arr 2=range ("A", "Z");//create an array of lowercase letters6 $arr 3=range ("A", "Z");//create an array of uppercase letters7 //Combine these three array groups into an array8 $arr 4=array_merge ($arr 1, $arr 2, $arr 3);9 Shuffle ($arr 4);//talk about array scramblingTen $arr 5=array_rand ($arr 4,6); One $arr 6=array_rand ($arr);//randomly remove four cells from an array and return one or more keys of a random entry (multiple returns an array, like this one that returns an array containing four elements) A $str 7= $arr 4[$arr 6[0]]. $arr 4[$arr 6[1]]. $arr 4[$arr 6[2]. $arr 4[$arr 6[3]];//combination - $str 8= $arr 4[$arr 5[0]]. $arr 4[$arr 5[1 ]]. $arr 4[$arr 5[2]]. $arr 4[$arr 5[3]]. $arr 4[$arr 5[4] [$arr 4[$arr 5[5]]; - echo "Your 4-digit verification code is:". $str 7. " <br/> "; the echo "Your 6 digit verification code is:". $str 8;

Execution Result:

Summary:1. In fact, this code is still a certain problem, because he is a one-time to take out four elements, the computer's output of this operation is that it takes the four elements will not have the same situation, but the reality of life is that we enter the verification code will be the same situation, such as two lowercase a, Two uppercase B, or two 1, which can be generated.

2. You can also do an optimization, that is, we can not consider the case, or the existence of a space, because we all know that when we do verification code verification, even if the display is uppercase, we output lowercase is also possible, of course, this through the built-in function can also be achieved.

3. Additional algorithm ideas. Actually do this case oneself also think of some other algorithm, also can achieve.

Conclusion : Maybe in some people who have better ideas may seem a little low, but for me just learning these functions, it is actually quite fun, so interested can also try to play.

Use PHP built-in functions to create a simple verification code

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.