0

我想知道是否有人可以帮助我的表单验证?

我在尝试同步脚本实际结构的某些部分如何协同工作时遇到了一些问题。

<?php
$flag="OK";   // This is the flag and we set it to OK
$msg="";        // Initializing the message to hold the error messages
   if(isset($_POST['Send'])){
      $key=substr($_SESSION['key'],0,4);
      $num_key = $_POST['num_key'];
      if($key!=num_key){
      $msg=$msg."Your Key not valid! Please try again!<BR>";
      $flag="NOTOK";
           }
      else{
    $msg=$msg."Your Key is valid!<BR>";
    $flag="OK";
        } 
         }
$email=$_POST['email'];
echo "Your Email: ".$email." is";
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
$msg=$msg."Invalid email<BR>";
$flag="NOTOK"; 
}else{
$msg=$msg."Valid Email<BR>";
$flag="OK";
}
$password=$_POST['password'];
if(strlen($password) < 5 ){ 
$msg=$msg."( Please enter password of more than 5 character length  )<BR>";
$flag="NOTOK"; 
}
if($flag <>"OK"){
echo "$msg <br> <input type='button' value='Retry' onClick='history.go(-1)'>";
}else{ // all entries are correct and let us proceed with the database checking etc …
} 
function spamcheck($field)
  {
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }
if (isset($_POST['email']))
  {//if "email" is filled out, proceed
  $mailcheck = spamcheck($_POST['email']); 
  if ($mailcheck==FALSE)
    {
    echo "Invalid input";
    }
      }
?>

问题,当电子邮件有效时,密码有效,虽然密钥无效密钥的警告消失了,这意味着也通过了......而且垃圾邮件检查看起来也不起作用..

4

2 回答 2

1

你们每个测试都将标志设置为“OK”或“NOTOK”,覆盖先前测试做出的决定。
你可以从 $flag = true; 开始。只有当测试确定输入不令人满意时,它才会设置 $flag=false。
或者您可以完全删除 $flag 并在测试后检查是否 0===strlen($msg) 。

于 2010-01-18T10:32:24.737 回答
1

正如您已经指出的那样,您不必将标志设置为“OK”或屏蔽先前的错误。如果所有检查都正常,则标志保持有效状态,您可以继续,否则,如果其中一项检查失败,则标志报告不正确的状态。

  $flag="OK";   // This is the flag and we set it to OK
  $msg="";        // Initializing the message to hold the error messages
  if(isset($_POST['Send'])) {
    $key=substr($_SESSION['key'],0,4);
    $num_key = $_POST['num_key'];
    if($key!=$num_key){
    $msg=$msg."Your Key not valid! Please try again!<BR>";
    $flag="NOTOK";
  } else {
    $msg=$msg."Your Key is valid!<BR>";
  } 
}

$email=$_POST['email'];
echo "Your Email: ".$email." is";
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
$msg=$msg."Invalid email<BR>";
$flag="NOTOK"; 
}else{
  $msg=$msg."Valid Email<BR>";
}

$password=$_POST['password'];
if(strlen($password) < 5 ){ 
  $msg=$msg."( Please enter password of more than 5 character length  )<BR>";
  $flag="NOTOK"; 
}

if($flag <>"OK"){
  echo "$msg <br> <input type='button' value='Retry' onClick='history.go(-1)'>";
} else { 
  // all entries are correct and let us proceed with the database checking etc …
} 

说我会使用不同的方法,例如使用布尔值而不是名为 flag 的字符串。您可以获得更流畅的代码,将其称为 $inputIsvalid。

其他唠叨:有时您将消息添加到 $msg 变量,其他您发出回声,也许这是一个疏忽。

有很大的改进空间,就像所有其他代码一样,我将只解决一些简单的问题,例如我不会检查变量是否已设置。

  $inputIsValid=true;   // This is the flag and we set it to OK
  $messages = array();        // Initializing the message to hold the error messages

  if(isset($_POST['Send'])) {
    $key=substr($_SESSION['key'],0,4);
    $num_key = $_POST['num_key'];
    if($key!=$num_key){
      $messages[]= 'Your Key not valid! Please try again!';
      $inputIsValid=false;
    } else {
      $messages[]'Your Key is valid!';
    } 
  }

  $email=$_POST['email'];
  $emailRegex='^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$';
  $emailIsValid = eregi($emailRegEx, $email);
  $messages[]= 'Your Email: '.$email.' is ' .($emailIsValid? 'Valid':'Invalid');
  $inputIsValid = $inputIsValid && emailIsValid;

  $password=$_POST['password'];
  if(strlen($password) < 5 ){ 
    $messages[]='( Please enter password of more than 5 character length  )';
    $inputIsValid=false; 
}

if(!inputIsValid){
  $messages[]='<input type='button' value='Retry' onClick='history.go(-1)'>';
  echo join('<br/>', $messages); 
} else { 
  // all entries are correct and let us proceed with the database checking etc …
} 

另一种方法应该是(功能很简单,但可以修改不同组件的验证策略而不影响主代码):

  function validateKey() {
    if(!isset($_POST['Send'])) {
      return true;
    }

    $key=substr($_SESSION['key'],0,4);
    $num_key = $_POST['num_key'];
    return $key==$num_key;
  }

  function validateEmail($email) {
    $emailRegex='^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$';
    return eregi($emailRegEx, $email);
  }

  function validatePassword($password) {
    return strlen($password) < 5;
  }

  $inputIsValid=true;   // This is the flag and we set it to OK
  $messages = array();        // Initializing the message to hold the error messages

  if(validateKey()) {
    $messages[]'Your Key is valid!';
  } else {
    $messages[]= 'Your Key not valid! Please try again!';
    $inputIsValid=false;
  }

  $emailIsValid = validateEmail($_POST['email']);
  $messages[]= 'Your Email: '.$email.' is ' .($emailIsValid? 'Valid':'Invalid');
  $inputIsValid = $inputIsValid && emailIsValid;

  $password=;
  if(!validatePassword($_POST['password']){ 
    $messages[]='( Please enter password of more than 5 character length  )';
    $inputIsValid=false; 
  }

if(!inputIsValid){
  $messages[]='<input type='button' value='Retry' onClick='history.go(-1)'>';
  echo join('<br/>', $messages); 
} else { 
  // all entries are correct and let us proceed with the database checking etc …
} 

垃圾邮件功能:

为什么你使用与布尔值不同的常量?(TRUE 与 true 不同,FALSE 与 false 不同)您可以像这样重写函数以获得所需的行为。

function spamcheck($field)
{
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);
  return filter_var($field, FILTER_VALIDATE_EMAIL);
}

if (isset($_POST['email'])) {//if "email" is filled out, proceed
  $mailcheck = spamcheck($_POST['email']); 
  if (!$mailcheck) {
    echo "Invalid input";
  }
}
于 2010-01-18T10:50:52.763 回答