PHP implementation generates an instance of a random string verification code

Source: Internet
Author: User
This article mainly introduces the use of PHP encapsulation function to generate random string verification code of the relevant data, the text gives a detailed introduction of the sample code, for everyone has a certain reference value, the need for friends can come together to learn.

Objective

In general, when we are doing the program, there will certainly be many places to use the random string, such as to do the verification code used, and then the function is encapsulated, use time to set 2 parameters, the principle is to randomly fetch the string, concatenation of the string

$str set the string to be collected, such as

$str =´jfowef34098094j3204efa234sfg2z23srhftj345xjxjhsrth´;

Then the string generated inside the function is randomly fetched from the $STR

$codeLen set the random string to be generated, set 5, then generate 5 random strings, such as

$codeLen =´5´;//Sets the number of random numbers generated

The code is as follows

<?php//mt_rand gets the random number Mt_rand (min, max);//sets the randomly collected string $str= " ABCDEFGHIJKMNPQRSTUVWXYZ0123456789ABCDEFGHIGKLMNPQRSTUVWXYZ ";//Set the number of random numbers generated $codelen=´5´;function Str_rand ($STR, $ Codelen) {$rand = ""; for ($i =0; $i < $codeLen-1; $i) {  //AS: Random number 30: $str [] $rand  . = $str [Mt_rand (0, strlen ($ STR)-1)];  } return $rand;} $code =str_rand ($str, $codeLen); Echo $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.