如何在表单中添加验证码?

时间:2014-05-21 04:29:10

标签: php

我是php的新手,我有一个包含搜索框的php表单,它将从我的数据库中搜索基因。但是在访问之前需要添加验证码。我不知道如何为例如添加验证码图像将显示一些数字或字母,用户必须键入这些数字或字母以便进一步处理。

创建表单的代码如下:

<form method="post" action="test3.php" name="search_form">
   <p align="right"><input type="text" name="search" size=15 maxlength=15 placeholder = "Gene Symbol">
    <select name="table[]" size = "0" multiple>
    <option selected="selected"></option>
    <option value="infla_info">Inflammation</option>
    <option value="diet_info">diet</option>
    <option value="obesity_info">obesity</option>
    <option value="stress_info">stress</option>
    <option value="atherosclerosis_info">atherosclerosis</option>
    <option value="retinopathy_info">Diabetic Retinopathy</option>
    <option value="nephropathy_info">Diabetic Nephropathy</option>
    <option value="neuropathy_info">Diabetic Neuropathy</option>
    </select>
    <input type="Submit" name="Submit" value="Gene Search">
    </p>
    </form>     

请帮助我!

1 个答案:

答案 0 :(得分:2)

reCAPTCHA 是个不错的选择。

<强>客户端

<?php
require_once('/path/to/recaptchalib.php');

$publickey = "your_public_key"; // you got this from the signup page
$recaptcha = recaptcha_get_html($publickey);
?>

<form method="post" action="test3.php" name="search_form">
    <p align="right">
        <input type="text" name="search" size=15 maxlength=15 placeholder = "Gene Symbol"/>
        <select name="table[]" size = "0" multiple>
            <option selected="selected"></option>
            <option value="infla_info">Inflammation</option>
            <option value="diet_info">diet</option>
            <option value="obesity_info">obesity</option>
            <option value="stress_info">stress</option>
            <option value="atherosclerosis_info">atherosclerosis</option>
            <option value="retinopathy_info">Diabetic Retinopathy</option>
            <option value="nephropathy_info">Diabetic Nephropathy</option>
            <option value="neuropathy_info">Diabetic Neuropathy</option>
        </select>

        <?php echo $recaptcha; ?>

        <input type="Submit" name="Submit" value="Gene Search"/>
    </p>
</form>    

服务器

<?php
require_once('/path/to/recaptchalib.php');
$privatekey = "your_private_key";
$resp       = recaptcha_check_answer ($privatekey,
                                      $_SERVER["REMOTE_ADDR"],
                                      $_POST["recaptcha_challenge_field"],
                                      $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")");
} else {
    // Your code here to handle a successful verification
}
?>