Undefine $ captcha如何解决?

时间:2016-07-24 04:28:07

标签: php

if (isset($_POST['btnSubmit']))
    {
        if(isset($_POST['g-recaptcha-response'])){
          $captcha=$_POST['g-recaptcha-response'];

        }
        echo $captcha;
        if(!$captcha){

          echo '<h2>Please check the the captcha form.</h2>';
          exit;
        }
        $secretKey = "================";
        $ip = $_SERVER['REMOTE_ADDR'];
        $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
        $responseKeys = json_decode($response,true);
        if(intval($responseKeys["success"]) !== 1) {
          echo '<h2>You are spammer ! Get the @$%K out</h2>';
        } else {
          echo '<h2>Thanks for posting comment.</h2>';
}

这是我的代码,当点击提交表单然后未定义变量$ chaptcha请帮帮我

1 个答案:

答案 0 :(得分:0)

如果引用尚未创建的变量,PHP会抛出通知,尽管代码仍然可以&#34;,Read this answer

尝试这样的事情:

<?php

if (isset($_POST['btnSubmit'])) {
    if(isset($_POST['g-recaptcha-response'])){
      $captcha=$_POST['g-recaptcha-response'];
      echo $captcha;
    }else {
      echo '<h2>Please check the the captcha form.</h2>';
      exit;
    }

    $secretKey = "================";
    $ip = $_SERVER['REMOTE_ADDR'];
    $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
    $responseKeys = json_decode($response,true);

    if(intval($responseKeys["success"]) !== 1) {
      echo '<h2>You are spammer ! Get the @$%K out</h2>';
    } else {
      echo '<h2>Thanks for posting comment.</h2>';
    }
}

?>