PHP代码练习,生成随机验证码

作者: JONE 分类: PHPer 发布时间: 2017-07-19 00:38
<?php
header("content-type:text/html;charset=utf-8");

        function rand_new(){
         $str=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q',
                  'r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L',
                    'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',0,1,2,3,4,5,6,7,8,9);
         
         //echo $str[mt_rand(0,61)],$str[mt_rand(0,61)],$str[mt_rand(0,61)],$str[mt_rand(0,61)];
         //mt_rand(0,61) 取随机数 区间0~61为$str下标的范围
         $a =$str[mt_rand(0,61)].$str[mt_rand(0,61)].$str[mt_rand(0,61)].$str[mt_rand(0,61)];
         
        
         return $a;
        }
         
        $b = rand_new();

        $c=$b;

        if(isset($_POST['sub'])){
                    $name=$_POST['name'];
                    $user=$_POST['user'];
             // 函数strtolower 把所有大写字符转成小写
            if(  strtolower($name)==strtolower($user)){  
                echo "答对了";
            }else{
                echo "答错了";
            }
            
        }
   //    var_dump($c);
?>
<form action="" method="post">
<input type="text" name="name" >
<input type="text"  disabled value="<?php echo isset($c)?  $c :  ""; ?>">
<!--带disabled属性从而导致默认值无法传值到php超全局数组中,故新建一个隐藏域用于传值-->
<input type="hidden" name="user"  value="<?php echo isset($c)?  $c :  ""; ?>">
<input type="submit"  name="sub" value="点击验证">
<form>

点击查看效果