验证用户名时出现PHP错误

时间:2014-10-29 09:49:52

标签: php validation debugging

我正在使用PHP构建一个网站,我需要验证用户输入的名称是否正确。由于JavaScript是客户端的,我不能完全依赖它,所以这是我的服务器端函数来验证用户名:

function validate_name($name) {
    $name = trim($name);  // only for the purpose of debugging <---- edited comment
    echo $name;
    if (strlen($name) <= 1) {
        return "small";
    } else if (has_numbers($name)) { 
        return "numbers";
    } else {
        return true;
    }
}

在此之后,我相应地检查输入并显示结果:

function final_check() {
    if (validate_name($_POST["first_name"]) == "small") {
        echo "<span class='error'>Your first name cannot be empty</span>";
        return false;
    } else if (validate_name($_POST["first_name"]) == "numbers") {
        echo "<span class='error'>Numbers are not allowed in your first name</span>";
        return false;
    }
    return true;
}

当我在first_name字段中输入任何内容时,我收到空错误消息;当我输入数字时,我得到数字错误消息。但是,当我输入有效名称时,它会给我输入名称为空的错误消息。

这是帖子数据:

Array
(
    [email] => ewf@gmail.com
    [first_name] => qwe
    [last_name] => wqe
    [password] => qwe
    [re_password] => qwe
    [gender] => Male
)

输出:

Your first name cannot be empty

知道我做错了什么吗?在过去的一小时里,我一直在努力解决这个问题,但我找不到解决办法。

5 个答案:

答案 0 :(得分:6)

Array
(
    [email] => ewf@gmail.com
    [first_name] => qwe
    [last_name] => wqe
    [password] => qwe
    [re_password] => qwe
    [gender] => Male
)

// First_name length = 3

function validate_name($name) {
    $name = trim($name);
    echo $name;
    if (strlen($name) <= 1) {
        return "small";
    } else if (has_numbers($name)) { 
        return "numbers";
    } else {
        return true;      // satisfy this case return true
    }
}

在这里它变得像

function final_check() {
    if (validate_name($_POST["first_name"]) == "small") {   // if(1 == 'small')
        echo "<span class='error'>Your first name cannot be empty</span>";
        return false;
    } else if (validate_name($_POST["first_name"]) == "numbers") {
        echo "<span class='error'>Numbers are not allowed in your first name</span>";
        return false;
    }
    return true;
}

if(1 ==&#39; small&#39;)它与Boolean的字符串比较总是会返回true。

请检查this page in the manual以了解问题。

答案 1 :(得分:1)

尝试

if (validate_name($_POST["first_name"]) == "small" && !validate_name($_POST["first_name"])) {
    echo "<span class='error'>Your first name cannot be empty</span>";
    return false;
} else if (validate_name($_POST["first_name"]) == "numbers" && validate_name($_POST["first_name"])) {
    echo "<span class='error'>Numbers are not allowed in your first name</span>";
    return false;
}

true == 'small'也会返回true

或尝试使用===将严格匹配值

if (validate_name($_POST["first_name"]) === "small") {
    echo "<span class='error'>Your first name cannot be empty</span>";
    return false;
} else if (validate_name($_POST["first_name"]) === "numbers"=) {
    echo "<span class='error'>Numbers are not allowed in your first name</span>";
    return false;
}

答案 2 :(得分:1)

然而,你的逻辑是正确的,正如其他问题所指出的那样,带有布尔比较的字符串将返回true。

要解决此问题,您应该使用===代替==,一切都应该没问题

答案 3 :(得分:1)

我建议您修改validate_name函数,使其只有一种返回类型,因为它当前可以返回stringboolean,这是不是一个好习惯。

例如,您可以执行以下操作:

function validate_name($name) {
    $name = trim($name);
    echo $name;
    if (strlen($name) <= 1) {
        return "small";
    } else if (has_numbers($name)) { 
        return "numbers";
    } else {
        return "ok"; // or return ""; or whatever string you want.
    }
}

此外,为了优化您的代码并提高可读性,您可以更改final_check功能,以便只使用中间变量调用validate_name

function final_check() {
    $valid = validate_name($_POST["first_name"]);
    if ($valid == "small") {
        echo "<span class='error'>Your first name cannot be empty</span>";
        return false;
    } else if ($valid == "numbers") {
        echo "<span class='error'>Numbers are not allowed in your first name</span>";
        return false;
    }
    return true;
}

答案 4 :(得分:-1)

我认为你必须只改变final_check()函数

function final_check() {
if(validate_name($_POST["first_name"])){
 return true;
}else{
    if (validate_name($_POST["first_name"]) == "small") {
        echo "<span class='error'>Your first name cannot be empty</span>";
        return false;
    } else if (validate_name($_POST["first_name"]) == "numbers") {
        echo "<span class='error'>Numbers are not allowed in your first name</span>";
        return false;
    }
  }
}