提交正确输入的验证码

时间:2013-03-21 12:33:56

标签: php

我有一个带有选项框的以下页面,当用户选择以下选项然后单击提交时,它会将它们带到相关的URL。但是,用户必须在框中键入正确的字符串,该字符串与屏幕上的简单静态验证码图像相匹配。

目前代码看起来像这样,但我需要帮助设置条件,以便在提交之前必须在框中键入“abcdef”,否则会出错。

<?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 ></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" >These are the following options:<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 ="www.msn.com">MSN</option>
        <option value ="www.youtube.com">Youtube</option>
        <option value ="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>


<tr>

</tr>
<tr>

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

1 个答案:

答案 0 :(得分:0)

首次显示页面时,将代码保存在会话中。然后根据会话变量检查代码字段的POST值。

session_start();
$_SESSION['captcha'] = 'abcdef';

表格提交:

session_start();
if($_POST['captcha'] == $_SESSION['captcha'] && isset($_POST['menu1'])){
    header();
}