如果设置了一个帖子值而另一个为空,则退出PHP

时间:2014-08-21 08:10:22

标签: php

是的,我一直在努力尝试,但我根本无法让它发挥作用。我有两个密码字段,用户需要匹配以使脚本继续。我已经有了这个:

if ($_POST['password']!= $_POST['password1'])
 {
     echo("Oops! Password did not match! Try again. ");
     exit();
 }
 else{
 }

所以这个脚本有效,除非用户没有设置password1的值,它仍然会继续执行脚本的其余部分,而不会退出。我想这与它的空/空或其他事实有关。

因此,如果password有值但password1为空,我制作了一个脚本来阻止它继续播放,但到目前为止,我还没有得到任何结果。

这是我到目前为止所做的:

 if (empty($_POST['password1']) && isset($_POST['password'])) {
      echo("Oops! Password did not match! Try again. ");
     exit();
} 
else{
}

7 个答案:

答案 0 :(得分:1)

试试这个:

if (!isset($_POST['password']) || !isset($_POST['password1'])) {
    $pass1 = trim($_POST['password']);
    $pass2 = trim($_POST['password1']);
    if (
        empty($pass1) ||
        empty($pass2) ||
        $pass1 !== $pass2
    ) {
        echo("Oops! Password did not match! Try again. ");
    }
}

答案 1 :(得分:0)

可能最好的方法是首先检查两个字段是否已设置而不是空,这可以一次性使用empty()完成。然后,检查两个字段是否彼此相等。

if (empty($_POST['password']) || empty($_POST['password1'])) {
    exit('Please fill in all password fields!');
}
if ($_POST['password'] !== $_POST['password1']) {
    exit('Password confirmation did not match!');
}

为安全起见,在使用$_POST值之前,您还应检查is_scalar()字段,以防止恶意用户在$_POST['password']中提交数组并制作服务器抛出错误。

我还建议使用mb_strlen()强制执行最小长度,以便用户使用至少六个字符作为密码。我们不需要检查两个密码字段的长度,因为我们会检查它们是否相互相等。

最后,您可以使用类似这样的代码:

if (empty($_POST['password']) || empty($_POST['password1'])
   || !is_scalar($_POST['password']) || !is_scalar($_POST['password1']))
{
    exit('Please fill in all password fields!');
}
if (mb_strlen($_POST['password']) < 6) {
    exit('Please use a password of at least 6 characters.');
}
if ($_POST['password'] !== $_POST['password1']) {
    exit('Password confirmation did not match!');
}

答案 2 :(得分:0)

使用empty(); !空($ _ POST [&#39; test&#39;])===(isset($ _ POST [&#39; test&#39;])&amp;&amp;!$ _ POST [&#39; test&#39 ;])

答案 3 :(得分:0)

if(isset($_POST['password'], $_POST['password1']))
{
     $password = trim($_POST['password']);
     $password1 = trim($_POST['password1']);
     if($password=="" || $password1=='')
     {
         echo "Oops! Field canot be blank !! ");
         exit;
     }
     else if($password != $password1)
     {
         echo "Oops! Password did not match! Try again. !! ");
         exit;
     }
     else
     { 
           // do here your code
     }
}
else
{
   echo "Oops! Parameter not set !! ");
   exit;
}

答案 4 :(得分:0)

这是你需要做的:

if (!isset($_POST['password']) || trim($_POST['password'])==="" ) {// If password field 1 is empty
      echo("Oops! Password is required! Try again. ");
     exit();
} 
if (!isset($_POST['password1']) || trim($_POST['password1'])==="" ) {// If password field 2 is empty
      echo("Oops! Password is required! Try again. ");
     exit();
} 
if ($_POST['password'] !== $_POST['password1'] ) {// If password field 1 &2 did not match
      echo("Oops! Password did not match! Try again.");
     exit();
} 
//Contiune....

答案 5 :(得分:0)

$password1 = $_POST['password1'];
$password = $_POST['password'];

 if(!empty($password) && !empty(password1))
    {
         if($password == $password)
         {
             // your executable code
             echo("Password match!");
         }
    } 
    else
    {
         echo("Oops! Password did not match! Try again. ");
         exit();
    }

答案 6 :(得分:0)

改变这个:

if (empty($_POST['password1']) && isset($_POST['password'])) {
      echo("Oops! Password did not match! Try again. ");
     exit();
} 
else{
}

为:

if (empty($_POST['password1']) || empty($_POST['password'])) {
      echo("please check ! at least one password is not filled. ");
     exit();
} 
else{
}