Google ReCaptcha V2的怪异行为

时间:2019-04-04 12:56:06

标签: php recaptcha

我正在使用PHP Mailer将表单的详细信息邮寄到特定的电子邮件地址。但是在此之前,代码将检查Google Recaptcha版本2的服务器端验证。我面临着这种奇怪的行为,即服务器验证始终使我返回false。我不知道为什么?我已经仔细检查了站点和秘密密钥,两者均与我的Google帐户相同。以下是代码:

    <?php 
    require 'PHPMailer-master/src/PHPMailer.php';
    require 'PHPMailer-master/src/SMTP.php';
    require 'PHPMailer-master/src/Exception.php';
    if(isset($_POST['submit'])) 
    {
        $captcha;
        $target_dir = "Upload_Attachment/";
        $name = htmlentities($_POST['name']);
        $email = htmlentities($_POST['email']);
        $mobile = htmlentities($_POST['mobile']);
        $edu_qual = htmlentities($_POST['edu_qual']);
        $years_exp = htmlentities($_POST['years_exp']);
        $comments = htmlentities($_POST['frmrequirements']);
        if(isset($_POST['g-recaptcha-response']))
        {
          $captcha=$_POST['g-recaptcha-response'];
        }
        if(!$captcha)
        {
          echo '<script>alert("Something Went Wrong!");</script>';
          exit;
        }
        $secretKey = "MY_SECRET_KEY";
        $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 '<script>alert("Something Went Wrong!");</script>';
          exit;
        } 
        else 
        {
        $ds= DIRECTORY_SEPARATOR;
        $target_dir = "resume_files".$ds;
        $target_file = $target_dir . basename($_FILES["my_File"]["name"]);
        if (move_uploaded_file($_FILES["my_File"]["tmp_name"], $target_file)) 
        {
        //echo "The file ". basename($file). " has been uploaded.";
        } 
        else 
        {
            echo '<script>alert("Something Went Wrong!");</script>';
        }
        $mail = new PHPMailer\PHPMailer\PHPMailer();
        $mail->isSMTP(); // enable SMTP
        $mail->SMTPAuth = true; // authentication enabled
        $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
        $mail->Host = "smtp.gmail.com";
        $mail->Port = 465; // or 587
        $mail->isHTML();
        $mail->Username = "MY_USER_NAME";
        $mail->Password = "MY_PASSWORD";
        $mail->SetFrom("MY_EMAIL");
        $mail->Subject = "Job Enquiry from ".$_POST['name'];
        $mail->Body = "
        <html>
        <body>
        <table cellspacing = '5' cellpadding = '5' border='2'>
        <tr>
        <td>Name:</td> 
        <td>".$name."</td>
        </tr>
        <tr>
        <td>Email ID:</td>
        <td>".$email."</td>
        </tr>
        <tr>
        <td>Mobile No:</td> 
        <td>".$mobile."</td>
        </tr>
        <tr>
        <td>Years of Experience:</td>
        <td>".$years_exp."</td>
        </tr>
        <tr>
        <td>Educational Qualification:</td>
        <td>".$edu_qual."</td>
        </tr>
        <tr>
        <td>Comments:</td>
        <td>".$comments."</td>
        </tr>
        </table>
        </body>
        </html>
        ";
        $mail->addAttachment($target_file);
        $mail->AddAddress("TARGET_EMAIL_ID");
        if(!$mail->Send()) 
        {
            echo '<script>alert("Something Went Wrong!");</script>';
        }
        else 
        {
        unlink($target_file);
        }
        echo "<script>location='careers?success=1'</script>";
        }
}
?>

请帮助我。

1 个答案:

答案 0 :(得分:0)

您似乎正在使用GET请求而不是POST来查询Google。
您可以使用#include <stdio.h> #include <stdlib.h> int main() { long num = 0; FILE *fptr; if ((fptr = fopen("test_num.txt","r+")) == NULL){ printf("Error! opening file"); return -1; } fscanf(fptr,"%ld", &num); // Increment counter by 1 num += 1; printf("%ld\n", num); fprintf(fptr, "%ld", num); fclose(fptr); return -1; } 建立POST请求。

示例:

Step 1: Open the file
Step 2: Read the long value from the file
Step 3: Increment the long value by 1
Step 4: Overwrite the long value of the file by new incremented value
Step 5: Close the file