未向用户显示错误消息


Error messages not being displayed to user

慢慢学习php还有很多工作要做。发现自己在书中有一点我认为是一个有用的练习编码的例子。我想检查用户密码的长度和复杂性。是的,我知道还有更长更复杂的密码空间。这个问题足够好,我可以慢慢学。要求是9个字符和1@符号。所有其他字符都是允许的。不确定密码不符合要求时为什么不返回错误消息。任何帮助都是感激的,并且知道这可能是一匹被击败的死马。这里的大多数其他答案都比我想要的更复杂,但最终会达到目的。请建设性地评论。。。谢谢

<?php
       $pwd = filter_input(INPUT_GET, 'password');
       $errmsg = "";
       //function with 2 parameters/one passed by reference
       function passVal($pwd) {
           $errmsg = null;
           if (!preg_match('/^(?=.*[@]){9}$/', $pwd)) {
                  $errmsg = "Password must contain exactly 9 characters and one @ sign.";
           }
           if (strlen($pwd == 9) && preg_match('/(?=.*[@])/', $pwd)) {
                 $errmsg = "Contains exactly 9 characters and there is at least one @ sign. Password is good";
           }
        return $errmsg;
      }
 ?>

HTML

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<header><h1>Password Check</h1></header>
<form action="" method="get">
<h3>Enter a password in the box.</h3><br>
<p>The password must be exactly 9 characters and include at least
one &#64 sign. All other characters are allowed.</p>
<p>Enter a password<input type="text" name="password"></p>
<p><button type="submit" formmethod="get" name="button">Check Password</button></p>
<p><?php echo $errmsg; ?></p>
</body>
</html> 

除了给出的其他答案。声明您从未调用过passVal()函数。

您对strlen()的条件语句失败了。

if (strlen($pwd == 9) && preg_match('/(?=.*[@])/', $pwd))

和应读作,并将$pwd包装在括号内:

if ((strlen($pwd) == 9) && preg_match('/(?=.*[@])/', $pwd))

按照手册http://php.net/manual/en/function.strlen.php关于CCD_ 4函数。

  • strlen($str);

所以你的(strlen($pwd == 9)会在这里失败。

因此,您可以使用$pwd参数添加并回显函数:

echo passVal($pwd);

之后

    return $errmsg;
}

然而,使用一个条件语句来检查它是否为空会更好。

旁注编辑:您似乎没有关闭表单,因此如果这是您的实际代码,则需要添加</form>


玩弄创意编辑

您还可以在检查条件empty()和在同一行内回显函数时使用三元运算符。

即:<p><?php echo !empty($pwd) ? passVal($pwd) : ''; ?></p>,并定义从$msg_bad$msg_good分配给$errmsg的两个不同消息。

这里有一个完整的重写:

<?php
$pwd = filter_input(INPUT_GET, 'password');
$msg_bad = "<b>Password must contain exactly 9 characters and one @ sign.</b>";
$msg_good  = "Contains exactly 9 characters and there is at least one @ sign. Password is good";

//function with 2 parameters/one passed by reference
function passVal($pwd) {
global $msg_good, $msg_bad;
    $errmsg = null;

    if ((strlen($pwd) == 9) && preg_match('/(?=.*[@])/', $pwd)) {
        $errmsg = $msg_good;
    }
    else {
        $errmsg = $msg_bad;
    }
     return $errmsg;
}

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<header><h1>Password Check</h1></header>
<form action="" method="get">
<h3>Enter a password in the box.</h3>
<p>The password must be exactly 9 characters and include at least
one &#64 sign. All other characters are allowed.</p>
<p>Enter a password<input type="text" name="password"></p>
<p><button type="submit" formmethod="get" name="button">Check Password</button></p>
</form>
<p><?php  echo !empty($pwd) ? passVal($pwd) : ''; ?></p>
</body>
</html>

检查下面的代码,我向下移动了$errmsg变量,并调用该函数为其分配返回值。

<?php
$pwd = $_GET['password'];
//function with 2 parameters/one passed by reference
function passVal($pwd) {
    $errmsg = null;
    if (!preg_match('/^(?=.*[@]){9}$/', $pwd)) {
        $errmsg = "Password must contain exactly 9 characters and one @ sign.";
        }
    if (strlen($pwd == 9) && preg_match('/(?=.*[@])/', $pwd)) {
         $errmsg = "Contains exactly 9 characters and there is at least one @ sign. Password is good";
}
    return $errmsg;
}
**$errmsg = passVal($pwd);**
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<header><h1>Password Check</h1></header>
<form action="" method="get">
<h3>Enter a password in the box.</h3><br>
<p>The password must be exactly 9 characters and include at least
one &#64 sign. All other characters are allowed.</p>
<p>Enter a password<input type="text" name="password"></p>
<p><button type="submit" formmethod="get" name="button">Check Password</button></p>
<p><?php echo $errmsg; ?></p>
</body>
</html> 

据我所知,您从未调用过passVal函数。你应该做这样的事情:

if( ! empty( $_GET ) ) {
    $errmsg = passVal( $_GET['password'] );
}

函数并不是因为你提交了表单就自动调用的。事实上,你应该始终检查表单是否已经提交,然后进行所需的处理。

理想情况下,您还应该将PHP逻辑与HTML分离,但这完全是另一个主题。

您没有调用函数,也不需要函数。这样做:

<?php
$pwd = filter_input(INPUT_GET, 'password');
$errmsg = "";
//function with 2 parameters/one passed by reference
if ((count($pwd)<9) && (strpos($pwd, "@") == false)) {
    $errmsg = "Password must contain exactly 9 characters and one @ sign.";
    }
else {
     $errmsg = "Contains exactly 9 characters and there is at least one @sign. Password is good";
}

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<header><h1>Password Check</h1></header>
<form action="" method="get">
<h3>Enter a password in the box.</h3><br>
<p>The password must be exactly 9 characters and include at least
one &#64 sign. All other characters are allowed.</p>
<p>Enter a password<input type="text" name="password"></p>
<p><button type="submit" formmethod="get" name="button">Check   Password</button></p>
<p><?php echo $errmsg; ?></p>
</body>
</html>