2016-10-14 38 views
-1

我正在PHP中构建一个网站,我需要验证用户输入为“新密码”和“确认密码”,但它不起作用。PHP验证算法

整页可在此link

$rstPwd1 = $_POST['rstPwd1']; 
$rstPwd2 = $_POST['rstPwd2']; 

if(empty($rstPwd1) && empty($rstPwd2)){ 
    echo "Please fill in both fields!"; 
} 
else{ 
    if(empty($rstPwd1) || empty($rstPwd2)){ 
     if(empty($rstPwd1)){ 
      echo "Please fill in 'New Password' field!"; 
     } 
     if(empty($rstPwd2)){ 
      echo "Please fill in 'Confirm Password' field!"; 
     } 
    } 
    else { 
     if ($rstPwd1 != $rstPwd2){ 
      echo "Passwords aren't matching!"; 
     } 
     else { 
      if(strlen($rstPwd1) <= 7) { 
       echo "Password must be minimum 8 characters long!"; 
      } 
     } 
    } 
} 
+0

当你说它不工作,什么究竟是不是按预期工作? – Terry

+0

@Terry我的意思是它不验证它。如果我们谈论流量,它甚至不会在第一块中获得。当我把两个字段都清空时,它会通过验证 –

+1

该脚本没有任何问题。我试了一下,并从$ rstPwd1 = $ _POST ['rstPwd1'];到$ rstPwd1 =“”;对于$ rstPwd1也是如此,它验证失败。这与你如何提交数据最相似 –

回答

2

如果我把你的验证规则的功能,有可能测试不同的输入,并查看它们是否按预期工作。通过一些值

<?php 
function reset_password_validator($password, $repeat_password) { 
    if(empty($password) && empty($repeat_password)){ 
     echo "Please fill in both fields!"; 
    } 
    else{ 
     if(empty($password) || empty($repeat_password)){ 
      if(empty($password)){ 
       echo "Please fill in 'New Password' field!"; 
      } 
      if(empty($repeat_password)){ 
       echo "Please fill in 'Confirm Password' field!"; 
      } 
     } 
     else { 
      if ($password != $repeat_password){ 
       echo "Passwords aren't matching!"; 
      } 
      else { 
       if(strlen($password) <= 7) { 
        echo "Password must be minimum 8 characters long!"; 
       } 
      } 
     } 
    } 
} 

执行命令

$inputs = array(
    array('',''), 
    array('foo', ''), 
    array('foo', 'bar'), 
    array('', 'bar'), 
    array('foo', 'foo'), 
    array('password', 'password') 
); 

foreach($inputs as $pair) 
{ 
    printf("Validating: '%s' and '%s'\n", $pair[0], $pair[1]); 
    reset_password_validator($pair[0], $pair[1]); 
    print "\n"; 
} 

输出:

Validating: '' and '' 
Please fill in both fields! 
Validating: 'foo' and '' 
Please fill in 'Confirm Password' field! 
Validating: 'foo' and 'bar' 
Passwords aren't matching! 
Validating: '' and 'bar' 
Please fill in 'New Password' field! 
Validating: 'foo' and 'foo' 
Password must be minimum 8 characters long! 
Validating: 'password' and 'password' 

我被诱惑与一般错误,以简化,并删除长度要求:

if(empty($password) || $password !== $repeat_password) { 
    $error = 'Password fields must match and not be empty.'; 
} 
1

你必须检查,当有人点击提交按钮。

如果有网站上的一些表格像

<form action="" method="get"> 
    ... 
    <input type="submit" name="submit_get1"> 
</form> 
<form action="" method="get"> 
    ... 
    <input type="submit" name="submit_get2"> 
</form> 
<form action="" method="post"> 
    ... 
    <input type="submit" name="submit_post1"> 
</form> 
<form action="" method="post"> 
    ... 
    <input type="submit" name="submit_post2"> 
</form> 

你可以用PHP访问它们如下:

if (isset($_GET["submit_get1"])) { 
    echo "submit_get1"; 
} 

if (isset($_GET["submit_get2"])) { 
    echo "submit_get2"; 
} 

if (isset($_POST["submit_post1"])) { 
    echo "submit_post1"; 
} 
if (isset($_POST["submit_post2"])) { 
    echo "submit_post2"; 
} 
+0

如果我在页面中使用这两种方法会怎么样? –

+0

请检查上面更新的链接,我的完整php页面可用 –