2013-03-21 64 views
0

我有以下頁面的選項框,當用戶選擇下列選項,然後點擊提交,它將他們帶到相關的網址。但是,用戶必須在與屏幕上的簡單靜態驗證碼圖像匹配的框中輸入正確的字符串。提交正確輸入驗證碼

目前代碼看起來像這樣,但是我需要幫助設置條件,以便在提交之前必須在框中鍵入「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> 

回答

0

當您第一次顯示頁面時,將代碼保存在會話中。然後根據會話變量檢查代碼字段的POST值。

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

表單提交:

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

我在哪裏可以把這個兩套代碼? – Remus 2013-03-21 13:40:38