dqdt45183 2014-01-03 17:40
浏览 50
已采纳

PHP ReCaptcha if else

OK I am trying to implement ReCaptcha on a simple form that posts to a DB. This form has a few different validation for validating the address is valid against LiveAddress API and deduping etc.

So I am trying to add ReCaptcha in the elseif like below, both of my other validation work but once it gets to ReCaptcha whether i enter a the captcha correct or incorrect when i hit submit it just goes to a blank page. With no errors.

I do have the correct public and private keys on my form and an includes for the recaptchalib.php. I can post that code if you think that would be helpful.

I am assuming that i need this to be in if else elseif below because if i just have all of the captcha code on the form like the example that you download it ignores the captcha all together it just get posted to the DB successfully, even if you don't put anything in the captcha input.

if(empty($errorMessage))
{
    // Dedupe the entry into the form
    $dupesql = "SELECT * FROM fromData WHERE (address = '$primary_number' AND city = '$city_name' AND state = '$state_abbreviation' AND zip = '$zipcode_full')";
    $duperaw = $mysqli->query($dupesql);

    // Check to make sure that they entered a valid address according to Liveaddress API
    if(empty($primary_number)){
        $bad_address .= "$bad_add in $bad_city, $bad_state is not a valid address please try again
";
    }
    // Check to make sure that they have not already registered deduping on address, city, state, zip
    elseif($duperaw->num_rows > 0) {
        $dupe .= "$full_name already exists on $bad_add in $bad_city, $bad_state 
";
    }

    elseif( isset( $_POST["recaptcha_response_field"] ) )
    {
        $resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);

        if(!$resp->is_valid) {
            echo "reCaptcha incorrect";
        } else {
            echo "reCaptcha OK";
        }
    }

    // If all "elseif" fail then we insert them into our database
    else {
        $sql = "INSERT INTO fromData (name, email, address, city, state, zip, date, optin, proccessed) VALUES ('$full_name', '$email', '$primary_number', '$city_name', '$state_abbreviation', '$zipcode_full', '$date', '$optin', 'No')";
        $mysqli->query($sql);
        echo $mysqli->error;

        header("location: index.php?success=1");
        exit();
    }
}

Can anyone see anything that is glaringly wrong with my code above? I have been moving the capcha code all around and it still isn't working. I am assuming that this is the closet i have to making it work the way that i need.

  • 写回答

1条回答 默认 最新

  • dtrn74832 2014-01-03 17:54
    关注

    The way you have it now, last block of code else { ... } only gets executed if there are no previous errors (which is good) and if user entered nothing for captha (which is bad).

    This is because if user enters anythnig for captcha, elseif( isset( $_POST["recaptcha_response_field"] ) ) will happen and the last else { ... } will not be executed.

    The code you want to execute if the captcha is correct should be in the

    else {
      echo "reCaptcha OK";        
    } 
    

    branch, not in the last else which only gets executed if variable which holds users response to captcha (I assumed you keep that in $_POST["recaptcha_response_field"]) is empty. So:

    elseif( isset( $_POST["recaptcha_response_field"] ) ) { 
      $resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"],     $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]); 
      if(!$resp->is_valid) { 
        echo "reCaptcha incorrect"; 
      }
      else {
        $sql = "INSERT INTO fromData (name, email, address, city, state, zip, date, optin, proccessed) VALUES ('$full_name', '$email', '$primary_number', '$city_name', '$state_abbreviation', '$zipcode_full', '$date', '$optin', 'No')";
        $mysqli->query($sql);
        echo $mysqli->error;
        header("location: index.php?success=1");
        exit();
      } 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DEV-C++编译缺失
  • ¥33 找熟练码农写段Pyhthon程序
  • ¥100 怎么让数据库字段自动更新
  • ¥15 antv g6 力导向图布局
  • ¥15 quartz框架,No record found for selection of Trigger with key
  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数
  • ¥15 关于#线性回归#的问题:【统计】回归系数要转化为相关系数才能进行Fisher' Z转化吗(相关搜索:回归模型)