PHP注册页面的逆向思维,菜鸟求解惑

<?php

<?php
header('content-type:text/html;charset=utf-8');
//1.接收数据
$username=$_POST['username'];
$password=$_POST['password'];
$password1=$_POST['password1'];
$email=$_POST['email'];
$age=$_POST['age'];
$verify=$_POST['verify'];
$verify1=$_POST['verify1'];
$reUrl="<a href='1-reg.php'>重新填写</a>";
// echo $username,'-',$password,'-',$password1,'-',$email,'-',$age,'-',$verify,'-',$verify1;
//2.检测用户名合法性,检测用户名长度
$userLen=strlen($username);
if($userLen>=6 && $userLen<=10){
    //检测用户名首字母是否是字母开始
    // $firstChar=$username{0};
    // $ascii=ord($firstChar);
    $ascii=ord($username{0});
    if(($ascii>=65 && $ascii<=90) || ($ascii>=97 && $ascii<=122)){
        //3.检测密码的合法性
        $pwdLen=strlen($password);
        if($pwdLen>=6 && $pwdLen<=10){
            //4.检测两次密码是否一致
            if($password===$password1){
                //5.检测邮箱的合法性
                $pos1=strpos($email,'@');
                $pos2=strrpos($email,'@');
                if($pos1!==false){
                    //检测@符号是否出现在起始或者结束的位置
                    if($pos1!=0 && $pos2!=(strlen($email)-1)){
                        //6.检测年龄的合法性
                        //检测是否是合法数值
                        if(is_numeric($age)){
                            //检测年龄范围
                            if($age>=1 && $age<=150){
                                //7.检测验证码是否符合规范
                                if($verify===$verify1){
                                    // echo '注册成功!!!!';
                                    $userInfo=<<<USERINFO
                                    <h1>注册成功,信息如下</h1>
                                    <table border="1" cellpadding="0" cellspacing="0" width="50%" bgcolor="#abcdef">
                                        <tr>
                                            <td>用户名</td>
                                            <td>{$username}</td>
                                        </tr>
                                        <tr>
                                            <td>邮箱</td>
                                            <td>{$email}</td>
                                        </tr>
                                        <tr>
                                            <td>年龄</td>
                                            <td>{$age}</td>
                                        </tr>
                                    </table>
USERINFO;
                            echo $userInfo;
                                }else{
                                    exit("验证码错误,{$reUrl}");
                                }
                            }else{
                                exit("年龄范围不符合规范,{$reUrl}");
                            }
                        }else{
                            exit("非法数值,{$reUrl}");
                        }
                    }else{
                        exit("邮箱中的@符号不能出现在起始或者结束位置,{$reUrl}");
                    }
                }else{
                    exit("邮箱中没有包含@符号,{$reUrl}");
                }
            }else{
                exit("两次密码不一致,{$reUrl}");
            }
        }else{
            exit("密码不符合要求,{$reUrl}");
        }
    }else{
        exit("用户名首字母不是以字母开始,{$reUrl}");
    }
}else{
    exit("用户名长度不符合规范,{$reUrl}");
}

如何在不改变整体结构的情况下调整局部的逻辑关系,来判断接收的数据是否正确

末峰
浏览 1482回答 4
4回答

仙士可

我第一次见过这么长的if,太他妈难受了if(验证密码错误){exit('')}if(验证另一个错误){exit('')}这样写不就行了,如果判断错误了,exit直接就退出了页面,至于包这么多层if么

慕圣4558451

看来这娃没学过正则

qq_歲月靜好不忘初心_0

过于复杂

子期不遇

这条件判断和提示真人性化!!有时候,你只需要总体概括一下,不需要这么详细的。
打开App,查看更多内容
随时随地看视频慕课网APP