PHP 计数器不断自行重置


PHP counter keeps resetting itself

所以基本上我有一个表单,将信息提交到我的网站,然后我可以稍后批准信息进入我的网站或删除它。

最近在网站上添加了一个 recaptcha 机器人检查器,这样我就可以避免垃圾邮件发送者等,但问题是我在同一文件中有提交页面的流程和布局(submitinfo.php)

recaptcha 过程会自动杀死页面,因为验证码不正确(因为他们没有机会输入它),所以我认为与其杀死页面,我只会让它想出一条消息说验证码不正确,但如上所述,当用户第一次打开页面时, 他们没有机会输入验证码,因此即使他们尚未输入任何内容,消息也会显示出来。

我想如果我添加一个计数器,该计数器仅在计数器大于 0 时才显示一条消息,并在每次加载页面时添加 +1。我这样做了,但即使验证码多次不正确,该消息也没有显示,我认为这是因为每次我按下提交按钮时计数器都会重置。这是代码,以便你们可以看到我实际做了什么:

$count
settype($count,"integer");
require_once('recaptchalib.php');
 $privatekey = "My Private Captcha Key goes here";
 $resp = recaptcha_check_answer ($privatekey,
                                 $_SERVER["REMOTE_ADDR"],
                                 $_POST["recaptcha_challenge_field"],
                                 $_POST["recaptcha_response_field"]);
// the stuff above is just the code from the captcha
 if (!$resp->is_valid) {
   // What happens when the CAPTCHA was entered incorrectly
   if($count>0){ $errormessage = "block"; //this displays the error message if the counter is greater then 0
   }
   $count++;
 } else{
    //here it submits the info

只需在检查验证码之前检查您是否有$_POST数据即可。它应该是这样的:

<?php
if ($_POST) {
    // check captcha
    if ($captcha_valid) {
        // persist submitted form data
        // redirect on success
    } else {
        $error = 'captcha';
    }
}
?>
<!-- display form -->