输出六个随机字符串

HTML代码

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 7     <title>Document</title>
 8 </head>
 9 <body>
10     <form action="verificaiton.php" method="POST" enctype="multipart/form-data">
11         验证码类型:<input type="text" name="types"><input type="submit" value="提交">
14     </form>
15 </body>
16 </html>

PHP代码

<?php
header("content-type:text/html;charset=utf-8");
$type = $_POST["types"];//接收提交过来的值
function type($type){//封装函数
$types = ["number","string","mixin"];//限制输入的类型
if(in_array($type,$types)){//判断是否在限制的类型里
    $number = "1234567890";//存所有的数字
    $string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";//存所有的字母
    $mixin = $number.$string;//存字母和数字
    $number = str_shuffle($number);//打乱所有的数字
    $string = str_shuffle($string);
    $mixin = str_shuffle($mixin);    
    if($type == number){//判断如果输入的是数字类型
        $colorVer = substr($number, 0,6);//截取前六位数字
        $colorVer = str_split($colorVer);//将数字字符串转成数组
        foreach ($colorVer as $key => $value) {//遍历数组
            $a = mt_rand(0,225);//存每个颜色的随机值
            $b = mt_rand(0,225);
            $c = mt_rand(0,225);
            echo "<font style=' color: rgb($a,$b,$c)'>$value</font>";//给数字数组的每个值添加随机颜色并输出
        }
    }else if($type == string){
        $colorVer = substr($string, 0,6);
        $colorVer = str_split($colorVer);
        foreach ($colorVer as $key => $value) {
            $a = mt_rand(0,225);
            $b = mt_rand(0,225);
            $c = mt_rand(0,225);
            echo "<font style=' color: rgb($a,$b,$c)'>$value</font>";
        }
    }else{
        $colorVer = substr($mixin, 0,6);
        $colorVer = str_split($colorVer);
        foreach ($colorVer as $key => $value) {
            $a = mt_rand(0,225);
            $b = mt_rand(0,225);
            $c = mt_rand(0,225);
            echo "<font style=' color: rgb($a,$b,$c)'>$value</font>";
        }
    }
}else {//如果所输入的值不在所限制的类型里,则提示请输入、、、并返回到输入界面
    echo "<script>";
    echo "alert('请输入number、string或mixin');";
    echo "window.location.href='ver.html';";
    echo "</script>";
}

}
type($type);
?>
原文地址:https://www.cnblogs.com/yuxiaoge/p/10685970.html