我有一个带有选项菜单的页面,但是希望用户通过验证码测试。目前我想要它,所以用户在文本框中输入至少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"> </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();
?>
答案 0 :(得分: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']);
}
}
?>
答案 1 :(得分:0)
您必须先将验证码保留在会话中,然后使用提交的值进行检查,以便在验证输入的值为3个字符或更多后匹配它们。