0

我有一个带有选项菜单的页面,但是希望用户通过验证码测试。目前我想要它,以便用户在文本框中输入至少 3 个字符,否则他们将无法提交表单,代码如下所示,表单末尾的 php 条件。但是我知道我显然弄错了。有没有办法做到这一点而不将 php 代码粘贴到另一个页面上?谢谢。

<?php
// Check to see if the form has been submitted.
if(isset($_POST['menu1'])) {
  // If the form has been submitted, force a re-direct to the choice selected.
  header('Location: ' . $_POST['menu1']);
}
?>




<div style="padding-left: 50px">
<p class="arial"><strong></strong><br /><br /></p>
<form method="post"> 
<table class="freecontact2form" border="0" width="400px">
<tbody>
<tr>
<td colspan="2"><span style="font-size: x-small;"> </span> <font color=#000000 >Which of the following do you want to use?</font>
  <br /><br /></td>
</tr>
<tr>
  <td valign="top"><table width="400px" class="freecontact2form">
    <tr>
      <td colspan="2"><br />
        <br />
        <div class="freecontact2formmessage"> </div></td>
    </tr>
    <tr>
      <td valign="top"><label for="menu1" >The options are:<span class="required_star"> </span></label></td>
      <td valign="top"><select name="menu1" id="menu1">
        <option selected="selected" value ="http://www.google.com">Google </option>
        <option value ="http://www.yahoo.com">Yahoo</option>
        <option value ="http://www.bing.com">Bing</option>
      </select></td>
    </tr>








    <tr>
      <td valign="top"><label for="captcha" ><span class="required_star"></span><span class="required_star"></span></label></td>
      <td valign="top"><BR /><BR /><img src="captcha.jpg" /></td>
    </tr>
    <tr>
      <td>Please enter the characters shown in the CAPTCHA image:</td>
      <td><input type="text" name="captcha" id="captcha" value="" size="10" />
      </td>
    </tr>







    <tr>
      <td style="text-align:center" colspan="2"><br /><br /> 
      <input type="submit" value=" Submit ">

 </td>
    </tr>
  </table></td>
  <td valign="top">&nbsp;</td>
</tr>



</tbody>
</table>
</form> <br />
<p> </p>
<p> </p>

<?php




if(isset($_POST['menu1'])) {


    function died($error) {
        echo "Sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    if(!isset($_POST['menu1']) ||

                !isset($_POST['captcha'])

        ) {
        died('Sorry, there appears to be a problem with your form submission.');        
    }

    $error_message = "";







  if(strlen($captcha) < 3) {
    $error_message .= 'Please ensure the captcha entered is at least 3 characters long';
  }


  if(strlen($error_message) > 0) {
    died($error_message);
  }



?>

<?php
}
die();
?>
4

2 回答 2

0

在文件顶部(而不是底部)尝试此代码。请注意,我修复了一些问题。

首先,您在运行验证代码之前进行了 header() 重定向,因为它位于文件顶部,而验证位于下方。我更改了逻辑,因此如果验证成功,则它将重定向,否则将显示错误消息

其次,您正在检查strlen($captcha)但应该检查strlen($_POST['captcha']).

<?php
function died($error) {
    echo "Sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}

if(isset($_POST['menu1'])) {
  if(!isset($_POST['menu1']) || !isset($_POST['captcha'])) {
      died('Sorry, there appears to be a problem with your form submission.');        
  }

  $error_message = "";

  if(strlen($_POST['captcha']) < 3) {
    $error_message .= 'Please ensure the captcha entered is at least 3 characters long';
  }

  if(strlen($error_message) > 0) {
    // VALIDATION ERRORS, DIE
    died($error_message);
  }
  else {
    // DO YOUR REDIRECT RIGHT HERE... NO VALIDATION ISSUES, CONTINUE...
    header('Location: ' . $_POST['menu1']);
  }
}
?>
于 2013-03-21T20:55:18.880 回答
0

您必须先将验证码保留在会话中,然后在验证输入的值是 3 个字符或更多字符后,将其与提交的值进行检查以匹配它们。

于 2013-03-21T20:59:19.850 回答