如何解决表单验证不同步的问题?

发布于 2024-08-18 04:54:15 字数 1650 浏览 1 评论 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";
    }
      }
?>

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

I am wondering if anyone out there can help with my form Validation Please?

I am having a few problems trying to synchronized out how certain bits of the actual structure of the script works together.

<?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";
    }
      }
?>

the problem, when email valid, password valid, though key is invalid the warning of key disappear, it mean passed too... and also the spamcheck doesn't look work..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑶笙 2024-08-25 04:54:15

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

  $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})

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

  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})

垃圾邮件功能:

为什么使用常数与布尔值不同?
(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";
  }
}
; $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 … }

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


垃圾邮件功能:

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


;
    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 不同)
您可以像这样重写该函数以获得所需的行为。

; $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 … }

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

垃圾邮件功能:

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

You don't have to set the flag to 'OK' or a previous error get masked, as you already noted.
If all the check are ok, the flag remains in valid state and you can pass on, otherwise, if one of the check fails the flag reports the incorrect state.

  $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 …
} 

Said that I would use a different approach, for example using boolean values other than a string named flag. You can obtain a more fluent code calling it something like $inputIsvalid.

Other nags: Sometimes you add the messages to a $msg variable, other you issue an echo, maybe it is an oversight.

There is a lot of room for improvements, as every other code, I will address just some of the easy issues, for examples I will not check if the variables are set or not.

  $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})

Another approach should be (the functions are quite simple, but you can modify the validation policy of the different components without affecting the main code):

  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})

Spam function:

why are you using Constant different than the boolena values?
(TRUE is different from true and FALSE is different from false)
You can rewrite the function like this in order to obtain the desired behaviour.

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";
  }
}
; $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 … }

Another approach should be (the functions are quite simple, but you can modify the validation policy of the different components without affecting the main code):


Spam function:

why are you using Constant different than the boolena values?
(TRUE is different from true and FALSE is different from false)
You can rewrite the function like this in order to obtain the desired behaviour.


;
    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 …
} 

Spam function:

why are you using Constant different than the boolena values?
(TRUE is different from true and FALSE is different from false)
You can rewrite the function like this in order to obtain the desired behaviour.

; $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 … }

Another approach should be (the functions are quite simple, but you can modify the validation policy of the different components without affecting the main code):

Spam function:

why are you using Constant different than the boolena values?
(TRUE is different from true and FALSE is different from false)
You can rewrite the function like this in order to obtain the desired behaviour.

懷念過去 2024-08-25 04:54:15

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

Each of you tests sets flag to "OK" or "NOTOK" overwriting decisions made by previous tests.
You could start with $flag = true;. And only if a test decides that the input is unsatisfying it sets $flag=false.
Or you can remove $flag altogether and check if 0===strlen($msg) after the tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文