谷歌ReCaptcha V2的怪异行为

问题描述 投票:0回答:1

我正在使用PHP Mailer将表单的详细信息邮寄到特定的电子邮件地址。但在此之前,代码检查了Google Recaptcha Version 2的服务器端验证。我正面临这种奇怪的行为,其中服务器验证总是返回我的错误。我无法弄清楚为什么?我已经仔细检查了网站和密钥,两者都是我定义的谷歌帐户。以下是代码:

    <?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>";
        }
}
?>

请帮我。

php recaptcha
1个回答
0
投票

看起来您正在使用GET请求而不是POST来查询Google。 您可以使用stream_context_create构建POST请求。

例:

$data = ['secret' => $secret, 'response' => $response, 'remoteip' => $ip];
$options = [
  'http' => [
    'header' => "Content-type: application/x-www-form-urlencoded\n",
    'method' => 'POST',
    'content' => http_build_query($data)
  ]
];

$context = stream_context_create($options);
$result = file_get_contents("https://www.google.com/recaptcha/api/siteverify", false, $context);
© www.soinside.com 2019 - 2024. All rights reserved.