<?PHP/** * Generate verification code * @param int $type * @param int $length * @return string*/functionBuildrandomstring ($type=1,$length=4){ if($type= = 1) { //The join () function is an alias for the implode () function, which is used to turn the array into a string, and the first parameter is the contents of the element placed between the groups of elements $chars=Join( "",Range(0, 9 ) ); } ElseIf($type= = 2) { //The range () function creates an array of elements that contain the specified range//array_merge () function merges one or more arrays into an array $chars=Join( "",Array_merge(Range("A", "Z"),Range("A", "Z" ) ) ); } ElseIf($type= = 3) { $chars=Join( "",Array_merge(Range("A", "Z"),Range("A", "Z"),Range(0, 9 ) ) ); } if($length>strlen($chars )) { Exit("String length is not enough" ); } $chars=Str_shuffle($chars);//the Str_shuffle () function randomly scrambles all characters in a string return substr($chars, 0,$length);//the substr () function returns the string of the specified part}
PHP Generate Image Verification code