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条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器