douyouyi8878 2013-09-05 14:58
浏览 102
已采纳

我正在一个非常大的表单上实现reCaptcha(在PHP中) - 如果reCaptcha失败,我如何保留表单数据?

Basically I have a gigantic form that would be a pain for anyone to have to re-fill. We have to implement reCaptcha to prevent spam bots.

I want to make it so that if the user enters the reCaptcha incorrectly, their form data is not lost.

The form, reCaptcha lib, and reCaptcha initialization code are in their own file titled Application.php. This form POSTs to application_confirm.php. application_confirm includes another file which processes the form data into an email format and sends it to the admin.

Application.php is required on another php page (ironically also titled application.php -- this is the one the user visits, and then the form html is included from the previously mentioned Application.php via require('content/Application.php')

I don't really know what I need to do to or where I need to place the captcha validity check -- any help much appreciated!

  • 写回答

4条回答 默认 最新

  • doulin6761 2013-09-05 15:49
    关注

    I like to store the $_POST data into a $_SESSION instead of a javascript approach. When the form is submitted, push the $_POST array into a $_SESSION variable. If the recaptcha fails, reload the form with the value from the $_SESSION. When the form is posted successfully, destroy the session so that if they were to return to the form, it isn't autofilled with old information.

    <?php
        session_start();
        $response = recaptcha_check_answer ($privatekey,
                $_SERVER["REMOTE_ADDR"],
                $_POST["recaptcha_challenge_field"],
                $_POST["recaptcha_response_field"]);
        if(isset($_POST['submit']) && !$response->is_valid)
        {
            $_SESSION['form'] = $_POST;
        }
        else
        {
            //Handle form processing
        }
    ?>
    

    Set value of input field to value from $_SESSION

    <input type="text" name="email" value="<?= $_SESSION['form']['email'] ?>" />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 本人复制了一个关于股票指标的代码,但是运行失败,有没有人帮我解决一下
  • ¥50 用matlab和numeca做透平机械流体力学和热力学模拟 价格可议
  • ¥15 Unity3D WebView
  • ¥20 论文AlphaTensor复现(有偿)
  • ¥15 (有偿)在ANSYS中 .anf文件
  • ¥45 关于#芯片#的问题:组合逻辑电路设计
  • ¥15 基与机器学习和时间序列分析预测养老服务需求趋势
  • ¥100 求连续两帧图像在水平和垂直上偏移
  • ¥15 Verilog hdl密码锁设计
  • ¥35 基于python的有ssl加密传输的socket聊天室